2013-04-24 120 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