原文网址: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