標籤:
原文網址: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【原創】