TableView是iOS开发中经常用到的View,针对不同的显示需求,我们需要不同的Cell来进行显示,比较复杂的显示我们一般会自定义Cell的样式,但是简单的显示就可以靠iOS本身支持的列表类型了。
iOS目前支持四中列表类型,分别是:
- UITableViewCellStyleDefault:默认类型,可以显示图片和文本
- UITableViewCellStyleSubtitle:可以显示图片、文本和子文本
- UITableViewCellStyleValue1:可以显示图片、文本和子文本
- UITableViewCellStyleValue2:可以显示文本和子文本
其显示的样式也各不相同,按顺序如下所示:
要设置也很简单,代码如下:
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell;
-
- switch (indexPath.row) {
- case 0:
- {
- NSString *CellOne = @"CellOne";
-
- cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellOne];
-
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.imageView.image = [UIImage imageNamed:@"icon"];
- cell.textLabel.text = @"textLabel";
- }
- break;
- case 1:
- {
- NSString *CellTwo = @"CellTwo";
-
- cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellTwo];
-
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.imageView.image = [UIImage imageNamed:@"icon"];
- cell.textLabel.text = @"textLabel";
- cell.detailTextLabel.text = @"detailTextLabel";
- }
- break;
- case 2:
- {
- NSString *CellThree = @"CellThree";
-
- cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellThree];
-
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.imageView.image = [UIImage imageNamed:@"icon"];
- cell.textLabel.text = @"textLabel";
- cell.detailTextLabel.text = @"detailTextLabel";
- }
- break;
- case 3:
- {
- NSString *CellFour = @"CellFour";
-
- cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellFour];
-
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.textLabel.text = @"textLabel";
- cell.detailTextLabel.text = @"detailTextLabel";
- }
- break;
- }
- return cell;
- }