【问题标题】:NSMutableArray Display Null Value in Table ViewNSMutableArray 在表格视图中显示空值
【发布时间】:2013-05-05 02:00:21
【问题描述】:
- (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;
}

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

【问题讨论】:

  • 尝试在向 detailArray 添加值后立即添加断点,以检查 detailArray 中是否有值。
  • 为什么还要在cellForRowAtIndexPath: 中将delegate 设置为tableview??还有为什么要在cellForRowAtIndexPath: 方法中重新加载tableview??从cellForRowAtIndexPath: method tableView.delegate = self 中删除这两行;和 [self.detailView reloadData];
  • Anila detailArray 具有值更正。
  • paras 我删除了那两行但没有改变。
  • @vishnusivabalan::: 在viewDidLoad 的末尾添加NSLog(@"%@",self.detailArray)[self.detailView reloadData];again 也调用了 tableview 委托方法。所以,可能是一个无限循环

标签: iphone xcode uitableview ios5 nsmutablearray


【解决方案1】:

更新

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

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

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

【讨论】:

  • 我删除了那两行但没有改变。请帮助我。
  • 设置 label.textColor = [UIColor blackColor];并在每个标签中设置文本测试尝试一下
  • 所以我认为你的方法cellForRowAtIndexPath 没有被调用做这件事只需在你的cellForRowAtIndexPath 方法中添加这一行NSLog(@"cellForRowAtIndexPath Called"); 告诉我这一行是否打印好..
  • 它没有打印。如果我在 detailarray 中给出常量值意味着它打印。
  • 是的,那么它的问题与你的数组老兄.. 我敢肯定.. 在添加值 init 后保留该数组,如 [self.detailArray retain];
【解决方案2】:

cellForRowAtIndexPath 方法中删除[self.detailView reloadData];。并以相同的方法删除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"];

【讨论】:

  • 我删除了那两行但没有改变。请帮助我。
  • print parsedJson 它打印什么?
  • 它显示两条路线之间的完整详细信息。例如:(lat,long,dis,route)。我从中提取路线值并存储在 detailArray 中。
  • 如果它不起作用,那么还要更改 NSArray *allkeys =(NSArray *) [parsedJson allKeys];
  • 我改变了所有没有变化。它不调用方法 cellForRowAtIndexPath。如果我在 detailarray 中使用常量值意味着它调用方法 cellForRowAtIndexPath。
【解决方案3】:

为什么要在 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    相关资源
    最近更新 更多