NSMutableArray在表视图中显示空值

时间:2013-05-10 11:35:37

标签: iphone xcode uitableview ios5 nsmutablearray

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

我想在Table View中显示detailArray。但是它在Table View中显示空值。但是在NSlog中它显示当前值。detailArray具有一组方向值。是否有可能在TableView中显示。如果我在displayArray中给出常量值,它在TableView中正确显示。如果有人知道,请帮助我。我在等你的宝贵答案,谢谢。

3 个答案:

答案 0 :(得分:1)

<强>已更新

在保留之后添加整个数据(值)并重新加载TableView,如下所示......

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

我相信你的问题已经解决了...... :)

答案 1 :(得分:1)

[self.detailView reloadData];方法移除cellForRowAtIndexPath。并以同样的方法删除tableView.delegate = self;

并且记住[super viewDidLoad];必须写在viewDidLoad的顶部。

修改  只需更改您的四个数组,如下所示。

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

答案 2 :(得分:0)

为什么要在viewDidLoad中设置detailView委托和数据源2次。请先格式化代码然后我们很容易描述问题。

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;