2013-03-25 98 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

相關問題