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关联。
@interface DemoCellTwoTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl; @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:];
}
cell.infoLabel.text = [self.datasource objectAtIndex:indexPath.row];
return cell;
}
这种方式不需要在viewcontroller的viewdidload方法里面注册重用的nib文件。只是在cell重用处加载nib。
注意:第二种方式,在自定义cell的xib文件中,file owner不需要修改,保持默认值就行了。

IOS自定义表格UITableViewCell的更多相关文章
- 【iOS自定义键盘及键盘切换】详解
[iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...
- 可任意自定义的 UITableViewCell
可任意自定义的 UITableViewCell UITableView的强大更多程度上来自于可以任意自定义UITableViewCell单元格.通常,UITableView中的Cell是动态的,在使用 ...
- iOS自定义的UISwitch按钮
UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...
- Android中使用ListView绘制自定义表格(2)
上回再写了<Android中使用ListView绘制自定义表格>后,很多人留言代码不全和没有数据样例.但因为项目原因,没法把源码全部贴上来.近两天,抽空简化了一下,做了一个例子. 效果图如 ...
- 如何实现 iOS 自定义状态栏
给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...
- Dev_GridView自定义表格
#region 自定义表格 //初始化测斜分析数据表 BandedGridView view = advBandedGridView1 as BandedGridView; view.BeginUpd ...
- iOS自定义组与组之间的距离以及视图
iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...
- iOS 自定义转场动画
代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...
- iOS 自定义转场动画浅谈
代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...
随机推荐
- jquery.easyui使用详解,和遇到的问题,提供大家在使用的时候少走弯路(二)
上次解释了几个易犯错的地方,当然对于大神们那都是小菜一碟了,今天来说说后台请求数据,分页,返回json数据 废话不多说献上代码 private string QueryList(ArrayList a ...
- 学习RaphaelJS矢量图形包--Learning Raphael JS Vector Graphics中文翻译(一)
(原文地址:http://www.cnblogs.com/idealer3d/p/LearningRaphaelJSVectorGraphics.html) 前面3篇博文里面,我们讲解了一本叫做< ...
- 转载:ORA-01438: 值大于为此列指定的允许精度
Number的数据声明如下: 表示 作用 说明Number(p, s) 声明一个定点数 p(precision)为精度,s(scale)表示小数 ...
- javscript创建Emitter
本文简单叙述下javascript是如何建立一个Emitter构造函数的. /** * 定义Emitter构造函数 */ function Emitter() { } /** * 添加监听事件 */ ...
- IIS错误:在唯一密钥属性 fileExtension=".url"时,无法添加类型为“mimeMap”的重复集合项
代码可以运行,但没有加载上样式,后来在IIS点击MIME类型时报错了 查看web.config中 <staticContent> <mimeMap fileExtension=&qu ...
- 1.4 云计算的SPI服务模型
云计算是通过共享资源池的方式来提高资源利用率的.在云计算中,根据其资源池中资源的类别,可以把云计算的服务模型分为三种,即所谓的SPI 模型 应用程序 Software as a Service ( ...
- js中toString和valueOf方法的区别
toString 方法 返回对象的字符串表示形式. 语法:objectname.toString([radix]) objectname 必需.要为其搜索字符串表示形式的对象. radix 可选.为将 ...
- swift开源项目精选
Swift 开源项目精选-v1.0 2016-03-07 22:11 542人阅读 评论(0) 收藏 举报 分类: iOS(55) Swift(4) 目录(?)[+] 转自 http: ...
- 基于OpenCV 的iOS开发
1.创建项目 2.https://sourceforge.net/projects/opencvlibrary/files/opencv-ios/2.4.13/opencv2.framework.zi ...
- 关于C#操作数据库ExecuteNonQuery()的返回值问题
) { retValue = AccessCon.ExecuteSql(sql = "update salesData set sellingPrize='" + man.Sell ...