2

Table View:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
return [self.detailArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
tableView.delegate = self;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 5.0f, 300.0f, 30.0f)];
label.text=[NSString stringWithFormat:@"%@",[self.detailArray objectAtIndex:indexPath.row]];
label.numberOfLines = 3;
label.font = [UIFont fontWithName:@"Helvetica" size:(12.0)];
label.lineBreakMode = UILineBreakModeWordWrap;
label.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:label];
[label release];
[self.myTableView reloadData];

return cell;

} I want to store the detail (array value) in the tableView.I used both method numberOfRowsInSection and cellForRowAtIndexPath but the table display null. How can I display?

4 Answers4

1

Make property for NSMutable array in .h

@property (nonatomic,retain) NSMutableArray *detailArray;

and synthesize it in .m

@synthesize detailArray=_detailArray;

and change this line NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;

to

 _detailArray=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;

then use _detailArray for displaying table data.

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    return [_detailArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// _detailArray objects for date population.
}
Ishu
  • 12,797
  • 5
  • 35
  • 51
1

Your Code :

NSString  *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",startPoint,midannotation.coordinate.latitude,midannotation.coordinate.longitude];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
    NSString *someString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    // NSLog(@"data:%@",someString);

    NSError* error;
    NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:data              options:kNilOptions  error:&error];

    NSArray *allkeys = [parsedJson allKeys];

    for(int i = 0; i < allkeys.count; i++){

        if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){
            arr  = [parsedJson objectForKey:@"routes"];
            dic   = [arr objectAtIndex:0];
            //  NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]);
            legs = [dic objectForKey:@"legs"];
            // NSLog(@"legs array count %d", legs.count);
            for(int i = 0;  i < legs.count; i++){
                stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"];
                for (int i = 0; i < stepsArr.count; i++) {
                    NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]);
                    NSLog(@"############################");
                    NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;
                }
            }

        }

    }        
});

first you go to your .h file -->

@property (nonatomic, strong) NSMutableArray * detailsArray;

In .m file -->

@synthesize detailsArray;

replace this code

NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;

by this -->

self.detailsArray = [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"];


# using table datasource methods 

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    return [self.detailsArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
 }

cell.titleLabel.text = [self.detailsArray objectAtIndex: indexPath.row ];

 return cell;
}

}

//Note Please set the delegate method of table , if you write the code programmatically means tableView.delgate = self;

Use this :

self.detaisArray = [[NSMutableArray alloc] init];
    // Do any additional setup after loading the view, typically from a nib.
    NSString  *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",startPoint,midannotation.coordinate.latitude,midannotation.coordinate.longitude];
    NSURL *googleRequestURL=[NSURL URLWithString:url];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
        NSString *someString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

        // NSLog(@"data:%@",someString);

        NSError* error;
        NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:data              options:kNilOptions  error:&error];

        NSArray *allkeys = [parsedJson allKeys];

        for(int i = 0; i < allkeys.count; i++){

            if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){
                NSArray *arr  = [parsedJson objectForKey:@"routes"];
                NSDictionary *dic   = [arr objectAtIndex:0];
                //  NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]);
                NSArray *legs = [dic objectForKey:@"legs"];
                // NSLog(@"legs array count %d", legs.count);
                for(int i = 0;  i < legs.count; i++){
                    NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"];
                    for (int i = 0; i < stepsArr.count; i++) {
                        NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]);
                        NSLog(@"############################");
                       [self.detaisArray addObject:[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ];
                        if(i == legs.count-1){
                            self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 400) style:UITableViewStylePlain];
                            self.myTableView.delegate = self;
                             self.myTableView.dataSource = self;
                            [self.view addSubview:self.myTableView];
                        }
                    }
                }

            }

        }        
    });
Tom Sawyer
  • 596
  • 5
  • 17
  • sorry to say this. its remain null value only display. – vishnu sivabalan May 09 '13 at 09:36
  • you see my NSLOG for "HTML instruction".its display direction route,is route value add in table or not? – vishnu sivabalan May 09 '13 at 09:38
  • Yes I want to see your NSLOG , and can you tell me what kind of value you want to print. Because the service returns Dictionary values, and multiple parameter. which parameter values you want to display. – Tom Sawyer May 10 '13 at 07:22
  • http://maps.googleapis.com/maps/api/directions/json?origin=Chennai&destination=Madurai&sensor=false. in this URl i took "html_instruction" value separately.i want to print that value.please help me. – vishnu sivabalan May 10 '13 at 07:26
  • Please see the updated one, I am tested in our end work exactly you want no null data. please check. – Tom Sawyer May 10 '13 at 08:50
  • Tom sawyer,when i click the buttom,the table is again null value only.but NSLog prpperly display on console.i display my tableview coding in the above .please help me,Thanks. – vishnu sivabalan May 10 '13 at 09:54
  • check you delgate or datasource method is called or not. if called then check your self.details array print all values or not, if yes then check youe cell property. – Tom Sawyer May 10 '13 at 10:30
  • If i give constant value in array means it display correctly.but i won't display the default values.i think may be problem in that array.help me.i am so confuse. – vishnu sivabalan May 10 '13 at 10:38
  • create a default array like that : NSMutableArray *mArray = [[NSMutableArray alloc]init]; after that you add like [ mArray addObject:[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ];, and send this array to tableview. – Tom Sawyer May 13 '13 at 08:23
0

first checked you connect your tableview delegate set self...

second checked youn row count pr detailarray cont in numberOfRowsInSection its may b zero

Sumit Mundra
  • 3,891
  • 16
  • 29
0

1st Declare Your Array in .h

like this

@property(strong,nonatomic)NSMutableArray * name;

And intialize into .m Init() method

name=[NSMutableArray alloc]initWithObjects:@"a",@"b", nil];  

// TableView Delegate Method To Display Array Data into Tableview.

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
        {
            return 1;   
        }

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
                return name.count;    //Give Your Array Name Here.  
        }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *CellIdentifier = @"Cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }

            cell.textLabel.text=[NSString stringWithFormat:@"%@",[name objectAtIndex:indexPath.row]];



            return cell;
        }

Try this code.

Jitendra
  • 5,055
  • 2
  • 22
  • 42