iOS中TableView的不同类型

来源:互联网 发布:旅程网络 官网 编辑:程序博客网 时间:2024/05/19 17:48

TableView是iOS开发中经常用到的View,针对不同的显示需求,我们需要不同的Cell来进行显示,比较复杂的显示我们一般会自定义Cell的样式,但是简单的显示就可以靠iOS本身支持的列表类型了。

iOS目前支持四中列表类型,分别是:

  • UITableViewCellStyleDefault:默认类型,可以显示图片和文本
  • UITableViewCellStyleSubtitle:可以显示图片、文本和子文本
  • UITableViewCellStyleValue1:可以显示图片、文本和子文本
  • UITableViewCellStyleValue2:可以显示文本和子文本

其显示的样式也各不相同,按顺序如下所示:


要设置也很简单,代码如下:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.     UITableViewCell *cell;  
  3.     // 共四种类型  
  4.     switch (indexPath.row) {  
  5.         case 0:// UITableViewCellStyleDefault:默认的类型,支持显示图片和文本  
  6.         {  
  7.             NSString *CellOne = @"CellOne";  
  8.             // 设置tableview类型  
  9.             cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellOne];  
  10.             // 设置不可点击  
  11.             cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  12.             cell.imageView.image = [UIImage imageNamed:@"icon"];// 图片  
  13.             cell.textLabel.text = @"textLabel";// 文本  
  14.         }  
  15.             break;  
  16.         case 1:// UITableViewCellStyleSubtitle类型,支持显示图片和文本以及子文本  
  17.         {  
  18.             NSString *CellTwo = @"CellTwo";  
  19.             // 设置tableview类型  
  20.             cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellTwo];  
  21.             // 设置不可点击  
  22.             cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  23.             cell.imageView.image = [UIImage imageNamed:@"icon"];// 图片  
  24.             cell.textLabel.text = @"textLabel";// 文本  
  25.             cell.detailTextLabel.text = @"detailTextLabel";// 子文本  
  26.         }  
  27.             break;  
  28.         case 2:// UITableViewCellStyleValue1类型,支持显示图片和文本以及子文本  
  29.         {  
  30.             NSString *CellThree = @"CellThree";  
  31.             // 设置tableview类型  
  32.             cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellThree];  
  33.             // 设置不可点击  
  34.             cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  35.             cell.imageView.image = [UIImage imageNamed:@"icon"];// 图片  
  36.             cell.textLabel.text = @"textLabel";// 文本  
  37.             cell.detailTextLabel.text = @"detailTextLabel";// 子文本  
  38.         }  
  39.             break;  
  40.         case 3:// UITableViewCellStyleValue2类型,支持显示文本以及子文本  
  41.         {  
  42.             NSString *CellFour = @"CellFour";  
  43.             // 设置tableview类型  
  44.             cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellFour];  
  45.             // 设置不可点击  
  46.             cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  47.             cell.textLabel.text = @"textLabel";// 文本  
  48.             cell.detailTextLabel.text = @"detailTextLabel";// 子文本  
  49.         }  
  50.             break;  
  51.     }  
  52.     return cell;  
  53. }  


可以在我的github获取示例工程:https://github.com/Cloudox/TableTypeDemo
0 0