2013-04-24 116 views
1

我試圖將信息顯示爲帶有2個動態單元的分組UITableView。每個分組的UITableView需要在單個視圖中調用10次;每個分組UITableView在其兩個單元之間顯示不同的信息。動態加載分組tableView

現在我試圖顯示存儲在posts中的數據庫中的所有數據。但是,當我運行此版本時,應用程序崩潰。如果我更改返回[self.posts count]numberOfSectionsInTableView:返回1,我只能加載一個部分,如預測。

我的問題是如何顯示10個分組的表格部分,每個部分有不同的信息?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
//#warning Potentially incomplete method implementation. 
// Return the number of sections. 
return [self.posts count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 


NSInteger returnValue = 2; 
switch (section) { 
    case 0: 
    { 
     returnValue = 2; 
     break; 
    } 
    default: 
     break; 
} 

return returnValue; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

NSString *CellIdentifier1 = @"Cell"; 


UITableViewCell *cell; 
if (indexPath.section==0) 
{ 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
    if (cell == nil) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]; 
    } 

    if (indexPath.row==0) 
      { 
       cell.textLabel.text = [[self.posts objectAtIndex:indexPath.row] objectForKey:@"name"]; 
      } 
    if (indexPath.row==1) 
      { 
       cell.textLabel.text = [[self.posts objectAtIndex:indexPath.row] objectForKey:@"message"]; 
      } 
} 

return cell; 

} 

回答

0

這是寫的代碼就足夠了您的具體情況。只需在cellForRowAtIndexPath中刪除此行if (indexPath.section==0){即可。

更新:根據你的觀點,以下代碼就足夠了。如果您正確存放到posts

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

NSString *CellIdentifier1 = @"Cell"; 


UITableViewCell *cell = nil; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
    if (cell == nil) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]; 
    } 

    if (indexPath.row==0) 
      { 
       cell.textLabel.text = [[self.posts objectAtIndex:indexPath.section] objectForKey:@"name"];//Here modified `section` instead of `row` 
      } 
    else if (indexPath.row==1) 
      { 
       cell.textLabel.text = [[self.posts objectAtIndex:indexPath.section] objectForKey:@"message"]; 
      } 


return cell; 

} 
+0

我能夠得到正確數量的部分。但是,它們都是相同的。我如何將正確的名稱和消息加載到每個部分? – williams 2013-04-24 04:41:46

+0

@williams看到我的upadated代碼.. – Mani 2013-04-24 04:55:12

+0

工程就像一個魅力。謝謝! – williams 2013-04-24 05:07:26

1

您在section = 0時創建單元格,您應該每次創建單元格。您不能從cellForRowAtIndexPath方法返回零。

將實施修改如下方式:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

    NSString *CellIdentifier1 = @"Cell";   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]; 
    } 

    if (indexPath.section==0) 
    { 
     if (indexPath.row==0) 
     { 
      cell.textLabel.text = [[self.posts objectAtIndex:indexPath.row] objectForKey:@"name"]; 
     } 
     //Rest of the code.. 
    } 
    else if(indexPath.section ==1) 
    { 
     //Do the work with section 1. 
    } 
    //do the work for other sections... 

    return cell; 
} 
+0

謝謝您的回答!現在,我將根據已存儲的帖子數獲取部分內容。但是,它們都是相同的。它在每個部分中加載相同的名稱和消息。如何正確加載每個部分以顯示來自我的數據庫的不同名稱和消息? – williams 2013-04-24 04:29:21

+0

我也可以使用你的輸入稍作修改cell.textLabel.text = [[self.posts objectAtIndex:indexPath.section] objectForKey:@「name」]; – williams 2013-04-24 05:10:39