2011-04-03 83 views
1

我正在处理一个tableview,在其单元格中显示多行文本。我想要两个部分,我已经完成了。但是在第二部分显示另一个数组是否可能不一样?牛逼关于分割你的UITableView

这是我使用的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
      if (section == 0) { 
      return MIN([titles count], [subtitles count]); 
      } 
      else { 
       return 1; 
      } 
     } 

回答

1

当然,你可以做到这一点。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 0) { 
     return MIN([titles count], [subtitles count]); 
    } 
    else { 
     return MIN([titles2 count], [subtitles2 count]); 
    } 
} 

只要确保在所有UITableViewDataSource和UITableViewDelegate方法中切换数组。


但我会建议嵌套数组。它使代码更短,因为当你有超过10个部分时,你的代码将变得漫长而丑陋。

而对于嵌套数组,您只有在viewDidLoad或无论您初始化您的数组中有长的难看的代码;

titles = [NSArray arrayWithObjects:titles0, titles1, nil]; 
subtitles = [NSArray arrayWithObjects:subtitles0, subtitles1, nil]; 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return MIN([[titles objectAtIndex:section] count], [[subtitles objectAtIndex:section]count]); 
} 
+1

我不得不改变的cellForRowAtIndexPath到\t'如果(indexPath.section == 0) \t { \t \t cell.textLabel.text = [标题objectAtIndex:indexPath.row]; \t \t cell.detailTextLabel.text = [subtitles objectAtIndex:indexPath.row]; \t} \t别的 \t \t如果(indexPath.section == 1) \t \t { \t \t \t cell.textLabel.text = [titles2 objectAtIndex:indexPath.row]; \t \t \t cell.detailTextLabel.text = [subtitles2 objectAtIndex:indexPath.row]; \t \t} \t \t return cell; '但现在一切正常。谢谢:) – mstottrop 2011-04-03 12:08:22

+1

多数民众赞成在我的意思是'只要确保在所有UITableViewDataSource和UITableViewDelegate方法切换数组.' :-) – 2011-04-03 12:42:31