【轉】iOS 通過xib自訂UITableViewCell【原創】

來源:互聯網
上載者:User

標籤:

原文網址:http://blog.it985.com/9683.html

在使用tableView的時候,如果cell的布局過於複雜,通過代碼搭建的話不夠直觀。並且要不停的調整位置,字型什麼的。這時,我們可以通過在tableViewCell的xib上搭建會更加直觀,有效提高開發效率。
首先,在我們建立了工程之後,建立XIB的cell。command+n,選擇Cocoa Touch Class

然後選擇UITableViewCell類型,同時鉤上Also Create xib File

之後,在對應的cell的xib上搭建我們需要的樣式

再在tableView中配合對應的代碼

123456789101112 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellIndentifier = @"MyTableViewCell";//這裡的cellID就是cell的xib對應的名稱    MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIndentifier];    if(nil == cell) {        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIndentifier owner:self options:nil];        cell = [nib objectAtIndex:0];    }         _tableView.rowHeight = cell.frame.size.height;//注意,這裡我們要把table的rowHeight設為和cell的高度一樣    return cell;}

之後我們來看下運行效果

最後,奉上demo
通過xib自訂UITableViewCell

【轉】iOS 通過xib自訂UITableViewCell【原創】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.