原文網址: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中配合對應的代碼
|
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;
}
|
最后,奉上demo
通過xib自定義UITableViewCell

