在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的更多相关文章

  1. 【iOS自定义键盘及键盘切换】详解

    [iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...

  2. 可任意自定义的 UITableViewCell

    可任意自定义的 UITableViewCell UITableView的强大更多程度上来自于可以任意自定义UITableViewCell单元格.通常,UITableView中的Cell是动态的,在使用 ...

  3. iOS自定义的UISwitch按钮

    UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...

  4. Android中使用ListView绘制自定义表格(2)

    上回再写了<Android中使用ListView绘制自定义表格>后,很多人留言代码不全和没有数据样例.但因为项目原因,没法把源码全部贴上来.近两天,抽空简化了一下,做了一个例子. 效果图如 ...

  5. 如何实现 iOS 自定义状态栏

    给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...

  6. Dev_GridView自定义表格

    #region 自定义表格 //初始化测斜分析数据表 BandedGridView view = advBandedGridView1 as BandedGridView; view.BeginUpd ...

  7. iOS自定义组与组之间的距离以及视图

    iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...

  8. iOS 自定义转场动画

    代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...

  9. iOS 自定义转场动画浅谈

    代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...

随机推荐

  1. java.sql.SQLException: Incorrect string value:

    安装好MySQL一定先改字符集 如果没有,改完字符集之后,要把之前数据库重新创建一下.

  2. 修复Windows XP右键没有新建菜单问题

    桌边上,点击鼠标右键,也没有排列图标菜单 开始-运行-输入:cmd输入命令:reg add "HKEY_CLASSES_ROOT\Directory\Background\shellex\C ...

  3. (实用篇)微信支付扫码支付php版

    本文实例为大家分享了php微信扫码支付源码,供大家参考,具体内容如下 代码中包含四个文件createUrl.php.ArrayToXML.php.returnGoodsUrl.php.notifyUr ...

  4. web发布 将各个文件夹输出合并到其自己的程序集 注意事项

    今天在发布web网站的时候 使用了“将各个文件夹输出合并到其自己的程序集”的选项,如图: 开始在 程序集前缀(可选)处,没有填写内容. 发布到IIS后出现未加载到程序集xxxx的错误. 经过各种调试, ...

  5. in a devstack Openstack env, how to start a service, such as aodh-listener

    in terminal, when start the service, the service will run in this terminal, and if kill this termina ...

  6. public/private/protected访问控制权限的区别

    //public/private/protected访问控制权限的区别//时间:2016/8/16 //(一)修饰成员: //public: 在类内.类外都能使用 . //protected: 在类内 ...

  7. ionic的tabs

    <ion-tabs class="tabs-icon-top/bottom(决定这个tabs是置于上面还是底部)  tabs-color-active-positive(图标与字体色) ...

  8. web安全之sql注入布尔注入

    条件: 当一个页面,存在注入,没显示位,没有数据库出错信息,只能通过页面返回正常不正常进行判断进行sql注入. 了解的函数 exists()                    用于检查  子查询是 ...

  9. android驱动开发前的准备(五)

    搭建S3C6410开发板的测试环境 首先安装串口调试工具 第一步:检测当前系统是否支持USB转串口 # lsmod | grep usbserial 第二步:安装minicom # apt-get i ...

  10. Django后台post请求中的csrf token

    使用Requests库操作自己的Django站点,post登陆admin页面返回403,serverlog显示csrf token not set. csrf token是get登陆页面时服务器放在c ...