【转】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