【问题标题】:iPhone UITableView populateing from connectionDidFinishLoadingiPhone UITableView 从 connectionDidFinishLoading 填充
【发布时间】:2010-02-06 21:15:18
【问题描述】:

我已经尝试了几个小时来解决这个问题。我有一些来自 NSURLConnection 的 JSON。这工作正常,我已将其解析为一个数组。但我似乎无法从 connectionDidFinishLoading 方法中取出数组。我在 UITableViewCell 方法中得到(null)。我假设这是保留问题的范围,但我对 ObjC 很陌生,我不知道该怎么做。任何帮助将不胜感激。
干杯。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];

SBJSON *json = [[SBJSON alloc] init];

NSDictionary *results = [json objectWithString:responseString];

self.dataArray = [results objectForKey:@"data"];

NSMutableArray *tableArray = [[NSMutableArray alloc] initWithObjects:nil];

for (NSString *element in dataArray) {
    //NSLog(@"%@", [element objectForKey:@"name"]);
    NSString *tmpString = [[NSString alloc] initWithString:[element objectForKey:@"name"]];
    [tableArray addObject:tmpString];
}
}

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

// Configure the cell.
//cell.textLabel.text = [self.tableView objectAtIndex:indexPath.row];

cell.textLabel.text = [self.tableArray objectAtIndex:indexPath.row];
NSLog(@"%@", tableArray);
return cell;

}

【问题讨论】:

    标签: iphone objective-c json uitableview nsurlconnection


    【解决方案1】:

    解决方案:
    在使用self.tableArray 听取 kubi 和 st3fan 的建议后,我发现我必须使用 [self.tableView reloadData]; 重新加载 tableView

    【讨论】:

    • 很高兴一切顺利!附言。您应该将我们的答案之一标记为正确。
    【解决方案2】:
    1. 去掉第二行的[connection release]connection 对象是自动释放的,因此这可能会导致崩溃。
    2. 看起来您有一个名为tableArray 的属性?如果是这样,您将在此方法中重新声明名称(您应该收到编译器警告)。

    再想一想,该方法的第 2 1/2 部分应如下所示:

    NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithObjects:nil];
    
    for (NSString *element in dataArray) {
        [tmpArray addObject:[element objectForKey:@"name"]];
    }
    
    self.tableArray = tmpArray;
    [tmpArray release];
    

    【讨论】:

    • 库比。我尝试过这种方式,但在 UITableVIewCell 方法中仍然为空?但我在 connectionDidFinishLoading 中得到了正确的值。它仍然卡在那里...感谢您的帮助。
    • 我确实注意到了这一点:2010-02-06 13:38:07.907 Courses[34368:207] (null) 2010-02-06 13:38:07.908 Courses[34368:207] (null) ) 2010-02-06 13:38:07.910 Courses[34368:207] (null) 2010-02-06 13:38:07.952 Courses[34368:207] (“我的高尔夫球场”、“Glen Annie”、“Alisal Ranch", "Santa Barbara Golf Club", "Rancho San Marcos" ) UITableViewCell 似乎在 connectionDidFinishLoading 之前加载。所以这些值还不存在......
    【解决方案3】:

    connectionDidFinishLoading: 中,您使用声明tableArray 作为局部变量。因此它永远不会分配给self.tableArray

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,我通过重新加载表格解决了它。

      我在connectionDidFinishLoading函数的末尾插入了这一行

      [self.tableView reloadData];    
      

      它有效。

      【讨论】:

        猜你喜欢
        • 2018-09-20
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多