iOS中TableView的不同類型
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:// UITableViewCellStyleDefault:預設的類型,支援顯示圖片和文本 { NSString *CellOne = @"CellOne"; // 設定tableview類型 cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellOne]; // 設定不可點擊 cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.imageView.image = [UIImage imageNamed:@"icon"];// 圖片 cell.textLabel.text = @"textLabel";// 文本 } break; case 1:// UITableViewCellStyleSubtitle類型,支援顯示圖片和文本以及子文本 { NSString *CellTwo = @"CellTwo"; // 設定tableview類型 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:// UITableViewCellStyleValue1類型,支援顯示圖片和文本以及子文本 { NSString *CellThree = @"CellThree"; // 設定tableview類型 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:// UITableViewCellStyleValue2類型,支援顯示文本以及子文本 { NSString *CellFour = @"CellFour"; // 設定tableview類型 cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellFour]; // 設定不可點擊 cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.textLabel.text = @"textLabel";// 文本 cell.detailTextLabel.text = @"detailTextLabel";// 子文本 } break; } return cell;}