IOS自定义表格UITableViewCell
在UITableView中,自定义表格,最原始是继承UITableViewCell,然后通过写代码方式去搞,但是这个费事了。
1.在storyboard中
给一个ViewController的tabieview增加自定义的UITableViewCell,可以直接从 object Library里面选取UITableViewCell拖动到tableview中,然后添加界面上自定义元素,然后补充cell的类,重用id等信息。
补充完成后,需要在工程中添加对应的cell的类文件,并做代码和xib的关联。
@interface DemoCellOne : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *imageInfoView; @property (weak, nonatomic) IBOutlet UILabel *contentInfoLabel;@end
然后就可以在相应的viewcontroller里面使用了,如下:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {DemoCellOne *cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCellOne"];cell.contentInfoLabel.text = [self.datasource objectAtIndex:indexPath.row];return cell; }
2.在普通的xib文件中
如果ios工程还是之前那种xib形式的,则可以给工程添加新文件,添加时候选择添加新的类,从UITableViewCell继承,然后在生成源码文件之前,先在确认界面勾选上生成对应的xib文件。
生成好之后,在xib中给UITableViewCell添加个性化元素,然后在代码中加载。
以下是cell对应的类的定义,为了便于修改,做了xib和代码之间的IBOutlet关联。
1 @interface DemoCellTwoTableViewCell : UITableViewCell 2 @property (weak, nonatomic) IBOutlet UILabel *infoLabel; 3 @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl; 4 5 @end
以下是在viewcontroller中使用
@interface ViewController2 ()<UITableViewDelegate,UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *contentTableView; @property(nonatomic,strong) NSMutableArray * datasource; @end@implementation ViewController2- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.[self.contentTableView registerNib:[UINib nibWithNibName:@"DemoCellTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"DemoCellTwoTableViewCell"];self.datasource = [NSMutableArray array];[self loadDataSource]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }- (void)loadDataSource {[self.datasource addObject:@"a1"];[self.datasource addObject:@"a2"]; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [self.datasource count]; }- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 60.0f; }- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {DemoCellTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCellTwoTableViewCell"];cell.infoLabel.text = [self.datasource objectAtIndex:indexPath.row];return cell; }@end
上面是通过注册tableview的cell对应的nib文件的方式来重用cell的。还有一种方式如下:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {DemoCellTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCellTwoTableViewCell"];if (nil == cell){NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"DemoCellTwoTableViewCell" owner:self options:nil];cell = [objs objectAtIndex:0];}cell.infoLabel.text = [self.datasource objectAtIndex:indexPath.row];return cell; }
这种方式不需要在viewcontroller的viewdidload方法里面注册重用的nib文件。只是在cell重用处加载nib。
注意:第二种方式,在自定义cell的xib文件中,file owner不需要修改,保持默认值就行了。
本文转自leipei博客园博客,原文链接:http://www.cnblogs.com/leipei2352/p/4358377.html,如需转载请自行联系原作者
IOS自定义表格UITableViewCell相关推荐
- ios cell点击对勾_带图像和对勾的iOS自定义TableView
ios cell点击对勾 In this tutorial, we'll be developing an iOS Application that contains a custom TableVi ...
- iOS自定义View 控件自动计算size能力
iOS自定义View 控件自动计算size能力 背景 在使用 UILabel 和 UIImage 的时候,不用指定宽高约束,控件也不会报约束缺失,还可以根据内容自己确定适合的宽高,特别适合 Xib 和 ...
- iOS——自定义cell
iOS--自定义cell 在写自定义cell怎么实现之前,先来看一下自定义cell的作用和用法,这一点远远比怎么实现有用的多,在进行了两天的网易云仿写后,才发现自己对自定义cell的理解完全是错的,按 ...
- iOS 自定义 UICollectionView汇总
文章目录 引言 I .iOS上传图片视图的封装[支持删除和添加] 1.1 demo源码下载: 1.2 计算cell的高度 II.风险商户材料证明视图 2.1 cellV的高度计算 2.2 自定义col ...
- 一劳永逸,iOS 自定义 ActionSheet 封装流程
原文链接:http://www.jianshu.com/p/cfb87a7db7b1 本文为 iOS 自定义视图封装<一劳永逸>系列的第四期,旨在提供封装思路,结果固然重要,但理解过程才最 ...
- R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:使用gtExtras包添加一个图,显示表中某一列中的数字、并自定义表格数据显示的主题格式、并自定义数值数据的格式(例如百分比)
R语言使用gt包和gtExtras包优雅地.漂亮地显示表格数据:使用gtExtras包添加一个图,显示表中某一列中的数字.并自定义表格数据显示的主题格式.并自定义数值数据的格式(例如百分比) 目录
- html生成自定义表格,自定义js的表格插件
场景:指定元素,生成自定义表格. 目的:了解js的插件开发. html代码: 自定义表格插件 var test = new MyTable({ elid:"mytable",//定 ...
- ios 自定义字体_如何仅用几行代码在iOS应用中创建一致的自定义字体
ios 自定义字体 by Yuichi Fujiki 藤木雄一 In this article, you'll learn how to create a unified custom look th ...
- iOS自定义组与组之间的距离以及视图
iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...
最新文章
- jmeter响应数据Unicode编码转换为汉字
- 基于vue和vuex的todos效果展示及源码分享
- 漫话:如何给女朋友解释什么是HTTP
- 微信扫码下载iosAPP
- 使用命令行刷新Magento索引管理 Rebuilt Magento Indexes in terminal with php-cli
- 【机器学习】机器学习从零到掌握之六 -- 教你使用验证分类器测试算法
- js系列教程4-函数、函数参数全解
- 实验五:大数据可视化工具-NodeXL
- 【抖音视频去水印小程序开源】mosousuo—微信小程序下载抖音去水印视频
- 宿主机上docker0 Linux 网桥设备是怎么来的?
- 11.Django中常用过滤器
- jsp:setProperty的用法
- 数据可视化-制作交易收盘价
- docker(12):Dockerfile
- 报名 微信 投票 php,女神微信投票PHP源码带PC报名和手机报名吸粉做微信活动必备...
- 详述数据中心内部通风的几种形态
- element控制input输入为数字
- kpw4换壁纸_kindle 篇五:kindle paperwhite4使用30天总结
- 计算机编程语言排行榜—TIOBE世界编程语言排行榜(2020年11月份最新版)
- cell-blog 功能介绍与安装