ios – 在UITableView中设置多个部分

前端之家收集整理的这篇文章主要介绍了ios – 在UITableView中设置多个部分前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经设置了自定义单元格的uitableview.

我想将这些分为标题部分.看下面的照片我正在寻找以下布局:

部分 – 我的个人资料
自定义单元格 – wwwwwwwwwwwwww …
部分 – 申请
自定义单元格 – 游戏
自定义单元格 – 分享
自定义单元格 – 速率
自定义单元格 – 设置
自定义单元格 – 帮助
自定义单元格 – 注销

我可以看到如何添加一个部分并控制一个部分中的行,但这会将单元格复制到多个部分,我不知道如何让一个部分有一行,另一个部分有6行.我也希望将这些部分的样式显示出来,就像Facebook菜单样式一样.

我应该为实际部分创建自定义单元格而不对部分(单元格)选择执行操作吗?

这是UITableView的代码

  1. static NSString *CellIdentifier = @"Cell";
  2. LeftMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  3.  
  4. if (cell == nil) {
  5.  
  6. NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"LeftMenuTableViewCell" owner:nil options:nil];
  7.  
  8. for (UIView *view in views) {
  9. if([view isKindOfClass:[UITableViewCell class]]) {
  10. cell = (LeftMenuTableViewCell*)view;
  11.  
  12.  
  13. }
  14. }
  15. }

解决方法

您可以在其中定义部分和行的数量,如下所示:
  1. - (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  2. {
  3. UIView view;
  4. if(section == 0) {
  5. // Initialise view for section 1
  6. } else {
  7. // Initialise view for section 2
  8. }
  9. }
  10.  
  11. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  12. {
  13. return 2;
  14. }
  15.  
  16. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  17. {
  18.  
  19. return ((section == 0) ? 1 : 6);
  20. }
  21.  
  22. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  23. {
  24. // return appropriate cell(s) based on section
  25. if(indexPath.section == 0)
  26. {
  27. // Return 1 cell
  28. }
  29. else if(indexPath.section == 1)
  30. {
  31. switch(indexPath.row) {
  32. case 0: // Initialize cell 1
  33. break;
  34. case 1: // Initialize cell 2
  35. break;
  36. ...
  37. }
  38. }
  39. return cell;
  40. }

猜你在找的iOS相关文章