UITableViewCell默认的单元有4种:
1.UITableViewCellStyleDefault
2.UITableViewCellStyleSubtitle 3.UITableViewCellStyleValue1 4.UITableViewCellStyleValue2首先要介绍下,默认的单元格使用了3种不同的元素。
- 图像:如果指定的样式中包含图像,那么该图像将显示在单元的文本左侧。
- 文本标签:单元的主要文本。
- 详细文本标签:单元的辅助文本,通常用作解释性的说明或标签。
如何使用这四种单元格呢?如下,修改红色的部分,可以使用其中一种单元格样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; if (cell == nil) { // 如果没有可重用的单元格对象,就使用标示符重新创建一个单元格对象, cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier]; } // 默认单元格包含的图片 cell.imageView.image = [UIImage imageNamed:@"star1"]; // 图片正常的状态 cell.imageView.highlightedImage = [UIImage imageNamed:@"star2"];// 图片被选中,高亮状态 // 默认单元格包含的文本 cell.textLabel.text = self.datas[indexPath.row]; // 默认单元格包含的详细文本标签 cell.detailTextLabel.text = [NSString stringWithFormat:@"detail %li", indexPath.row+1]; return cell;}
运行截图
可以看到,图片设置高亮状态,选中后显示不一样的图片。可是,为什么详细标题没有呢?
这是因为使用的样式是UITableViewCellStyleDefault,这个样式不显示详细标题的。下面分别是其它3种样式的运行情况:
2.UITableViewCellStyleSubtitle
3.UITableViewCellStyleValue1
4.UITableViewCellStyleValue2,不显示图片,所有文本右对齐,所有详细文本左对齐。