【轉】iOS 通過xib自定義UITableViewCell【原創】


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

在使用tableView的時候,如果cell的布局過於復雜,通過代碼搭建的話不夠直觀。並且要不停的調整位置,字體什么的。這時,我們可以通過在tableViewCell的xib上搭建會更加直觀,有效提高開發效率。
首先,在我們創建了工程之后,新建XIB的cell。command+n,選擇Cocoa Touch Class
屏幕快照 2015-04-07 下午10.54.49
然后選擇UITableViewCell類型,同時鈎上Also Create xib File
屏幕快照 2015-04-07 下午10.55.30
之后,在對應的cell的xib上搭建我們需要的樣式
屏幕快照 2015-04-07 下午11.03.00
再在tableView中配合對應的代碼

1
2
3
4
5
6
7
8
9
10
11
12
- (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;
}

之后我們來看下運行效果
屏幕快照 2015-04-07 下午11.03.40

最后,奉上demo
通過xib自定義UITableViewCell


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM