原文网址:http://www.kancloud.cn/digest/ios-1/107420

上一节中,我们定义的cell比较单一,只是单调的输入文本和插入图片,但是在实际开发中,有的cell上面有按钮,有的cell上面有滑动控件,有的cell上面有开关选项等等,具体参加下面2个图的对比:
         

@我们可以通过2种方式来实现自定义,一是利用系统的UITableViewCell(但不推荐,因为开发效率不高),举例:还是在这个关键方法中

(UITableViewCell)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath)indexPath{

    static NSString cellIdentifier  = @"cell";

    UITableViewCell cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier];

    if(!cell) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];

// 首先,各种按钮控件的初始化,一定要放在这个if语句里面,如果放在这个大括号外面,cell被执行n次,那么一个cell上面就会被添加n个控件

// 其次,你在这个括号里面自定义初始化控件后,如果你想给每一个cell的控件显示不同的内容和效果,你在括号外面是取不到对象的,只有通过设置它们继承UIView的属性tag来标识,我们可以想一想,如果控件一多或者别人来接受你的项目,你自己定义了很多的tag,这样合作的效率不高,所以主要推荐第二种

}
return cell;
}

@二是,创建UITableViewCell子类,在contentView上实现自定义效果(cell上的所有内容都是显示在cell的属性contentView上),这里也是写这个方法

#import <UIKit/UIKit.h>

@interface HMTAssistCell : UITableViewCell

@property (nonatomic) UILabel * label;
@property (nonatomic) UIButton * button; @end - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) { _button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.backgroundColor = [UIColor redColor];
_button.frame = CGRectMake(150, 60, 50, 100);
[_button setTitle:@"油条" forState:UIControlStateNormal];
[self.contentView addSubview:_button]; _label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 100, 100)];
_label.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:_label]; }
return self;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 依旧设置重用标识
static NSString * cellIdentifier = @"cell"; // 这里用我们新建的UITableViewCell的子类进行cell重用声明
HMTAssistCell * cell = (HMTAssistCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; // 如果没有,则创建
if (!cell) { cell = [[HMTAssistCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // 因为_label是类HMTAssistCell中的属性,所以就能很方便的取出来进行赋值
cell.label.text = [NSString stringWithFormat:@"%d",indexPath.row]; return cell;
}

【转】UITableView详解(UITableViewCell的更多相关文章

  1. UITableView详解(1)

    一,UITableView控件使用必备,红色部分是易错点和难点 首先创建一个项目,要使用UITableView就需要实现协议<UITableViewDataSource>,数据源协议主要完 ...

  2. UITableView 详解 ()

    (原本取至D了个L微信公众号) UITableView 详解 一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRec ...

  3. UITableView详解(转)

    首先.对UITableView进行讲解,下面有对它进行实际的应用 UITableView 显示大型内容的列表 单行,多列 垂直滚动,没有水平滚动 大量的数据集 性能强大,而且普遍存在于iPhone的应 ...

  4. UITableView详解(2)

    承接上文,再续本文 一,首先我们对上次的代码进行改进,需要知道的一点是,滚动视图的时候,我们要创建几个视图,如果一个视图显示一个图片占据整个屏幕,对于滚动视图我们只需要创建三个视图就可以显示几千给图片 ...

  5. UITableView 详解 教程

    看TableView的资料其实已经蛮久了,一直想写点儿东西,却总是因为各种原因拖延,今天晚上有时间静下心来记录一些最近学习的TableView的知识.下面进入正题,UITableView堪称UIKit ...

  6. UITableView详解

    一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(, , , )]; [DataTable setD ...

  7. 《iOS 7 应用开发实战详解》

    <iOS 7 应用开发实战详解> 基本信息 作者: 朱元波    管蕾 出版社:人民邮电出版社 ISBN:9787115343697 上架时间:2014-4-25 出版日期:2014 年5 ...

  8. iPhone应用开发 UITableView学习点滴详解

    iPhone应用开发 UITableView学习点滴详解是本文要介绍的内容,内容不多,主要是以代码实现UITableView的学习点滴,我们来看内容. -.建立 UITableView DataTab ...

  9. IOS中表视图(UITableView)使用详解

    IOS中UITableView使用总结 一.初始化方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styl ...

随机推荐

  1. uva 11076

    计算出每一位上数字i会出现的次数  累加 #include <cstdio> #include <cstdlib> #include <cmath> #includ ...

  2. mahout安装配置

    1.下载mahout 下载地址:http://mahout.apache.org 我下载的最新版:mahout-distribution-0.9 2.把mahout解压到你想存放的文档,我是放在/Us ...

  3. 1050 Moving Tables

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. [转]Ubuntu 12.04 安装屏保

    From:http://www.howtogeek.com/114027/how-to-add-screensavers-to-ubuntu-12.04/ How to Add Screensaver ...

  5. [GCJ]Password Attacker

    https://code.google.com/codejam/contest/4214486/dashboard#s=p0 排列组合.DP递推式,如下代码.dp[m][n]表示长度为n的字符串里有m ...

  6. .net与linux

    Mono是什么? http://blog.sina.com.cn/s/blog_693ffe760100k5uh.html Mono是一个开放源代码,Linux版的Microsfot.NET开发平台 ...

  7. mysql建表时拆分出常用字段和不常用字段

    一对一 一张表的一条记录一定只能与另外一张表的一条记录进行对应,反之亦然. 学生表:姓名,性别,年龄,身高,体重,籍贯,家庭住址,紧急联系人 其中姓名.性别.年龄.身高,体重属于常用数据,但是籍贯.住 ...

  8. The Introduction of Java Memory Leaks

    One of the most significant advantages of Java is its memory management. You simply create objects a ...

  9. OSSEC配置文件ossec.conf中添加mysql服务

    配置路径:/opt/ossec/etc/ossec.conf <ossec_config>   <global>     <email_notification>y ...

  10. Android ActionBar的Overlay模式如何不遮盖顶部内容的问题

    关于actionbar的overlay模式请参考 如何让android的actionbar浮动且透明 一文.这篇文章讲的是如何在这种模式下让actionbar不遮住顶部的内容. 这 一般是这样的场景, ...