vishnu sivabalan
vishnu sivabalan

Reputation: 97

NSMutableArray Display Null Value in Table View

- (void)viewDidLoad {

self.detailView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)    style:UITableViewStylePlain];
self.detailView.dataSource = self;
self.detailView.delegate = self;
self.detailView.multipleTouchEnabled=YES;
[self.view addSubview:self.detailView];
[super viewDidLoad];
self.title = NSLocalizedString(@"Routes", nil);
self.detailArray = [[NSMutableArray alloc] init];
NSString  *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=Chennai&destination=Madurai&sensor=false"];
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];
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];
NSArray *legs = [dic objectForKey:@"legs"];
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.detailArray addObject:[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ];
                }
            }

        }

    }        
});    
}

-(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];
}


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];

return cell;
}

i want to display the detailArray in the Table View.But it display null values in Table View.But in NSlog it display current values.The detailArray having set of direction values. is any possible to display in TableView.if i give constant value in displayArray its show correctly in TableView. please help me if Any one know.i am waiting for your valuable Answers,Thanks.

Upvotes: 1

Views: 711

Answers (3)

Paras Joshi
Paras Joshi

Reputation: 20541

UPDATED

After add whole data (values) in after retain it and reload TableView like bellow...

[self.detailArray retain];
[self.detailView reloadData];

i am sure your problem solved... :)

Upvotes: 1

SAMIR RATHOD
SAMIR RATHOD

Reputation: 3510

Remove [self.detailView reloadData]; from cellForRowAtIndexPath method. and also remove tableView.delegate = self; in same method.

and remember [super viewDidLoad]; must write at top of viewDidLoad.

Edit just change your four array as below.

   NSArray *legs=(NSArray *)[dic objectForKey:@"legs"];
   NSArray *arr  = (NSArray *)[parsedJson objectForKey:@"routes"];
   NSArray *legs = (NSArray *)[dic objectForKey:@"legs"];
   NSArray *stepsArr = (NSArray *)[[legs objectAtIndex:i] objectForKey:@"steps"];

Upvotes: 1

iEinstein
iEinstein

Reputation: 2100

Why are you setting detailView delegate and datasource 2 times in viewDidLoad. Please format the code first then it will be easy for us to describe the problem.

self.detailView.dataSource=self;
self.detailView.delegate=self;
self.detailView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)    style:UITableViewStylePlain];
self.detailView.dataSource = self;
self.detailView.delegate = self;

Upvotes: 0

Related Questions