iOS中TableView的不同類型

來源:互聯網
上載者:User

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;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.