码迷,mamicode.com
首页 > 移动开发 > 详细

【转】iOS 通过xib自定义UITableViewCell【原创】

时间:2016-06-22 23:36:29      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

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

【转】iOS 通过xib自定义UITableViewCell【原创】

标签:

原文地址:http://www.cnblogs.com/wi100sh/p/5608815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!