2013-03-25 172 views
0

我用它来创建UITableView,并使用NSArray创建标签。遵循教程here。现在我试图创建UITableView从我的服务器提取动态内容。数据的拉动与JSON的作品很好,我指定为这样:UITableView动态单元格内容

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString *responseString1 = [[NSString alloc] initWithData:responseData1 encoding:NSUTF8StringEncoding]; 

    NSLog(@"%@",responseString1); 

    SBJsonParser *parser = [[SBJsonParser alloc] init]; 
    id sample = [parser objectWithString:responseString1]; 

    tempholder=[NSMutableArray arrayWithCapacity:[sample count]]; 

    for (int i = 0; i < [sample count]; i++) { 
     [tempholder addObject:[[sample objectAtIndex:i]objectAtIndex:0]]; 
     //NSLog(@"%@",[sample objectAtIndex:i]); 
     NSLog(@"%@",[tempholder objectAtIndex:i]); 
    } 
} 

那么,如何更换

tableData = [NSArray arrayWithObjects:@"Item", @"Item", 
      @"Item", @"Item", @"Item", @"Item", @"Item", 
      @"Item", @"Item", nil]; 

这是我

- (void)viewDidLoad 

与“tempholder” NSMutableArray

Thanx提前...

+0

是资料表使用加载你的细胞中的cellForRowAtIndexPath阵列? – Petar 2013-03-25 15:41:45

回答

1

将tempHolder数组设置为您的tableData,然后在connectionDidFinishLoading:方法结束时重新加载表视图。

tableData = tempholder; 
[self.tableView reloadData]; 
1

你做你解析JSON后,补充一点:

self.tableData = [NSArray arrayWithArray:tempholder]; 

然后你就会probalby要relaod你的表。

1

你几乎可以在任何你使用NSArray的地方使用NSMutableArray。将你的tableData引用替换为tempholder(或者重命名tempholder使其更有意义)。相反,如果你真的需要,你也可以设置tableData为tempholder。

1
在你的代码

你必须初始化的NSArray在viewDidLoad中实现方法具

tableData = [NSArray arrayWithObjects:@"Item", @"Item", @"Item", @"Item", @"Item", @"Item", @"Item", @"Item", @"Item", nil]; 

,而不是NSArray的使用NSMutableArray里吧。

那么当您收到来自服务器的数据,只是下面

[tableData removeAllObjects]; 
tableData = tempholder 
+0

,因为你不能编辑NSArray,而NSMutableArray允许稍后编辑。 – 2013-03-25 15:46:59

+0

不能使用[tableData removeAllObjects] ...有一个SIGBART错误... – 2013-03-25 16:22:27

+0

它必须是NSMutableArray类型,那么你可以removeAllObjects。 – 2013-03-25 18:16:36

相关问题