【转】UITableView详解(UITableViewCell
原文网址: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的更多相关文章
- UITableView详解(1)
一,UITableView控件使用必备,红色部分是易错点和难点 首先创建一个项目,要使用UITableView就需要实现协议<UITableViewDataSource>,数据源协议主要完 ...
- UITableView 详解 ()
(原本取至D了个L微信公众号) UITableView 详解 一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRec ...
- UITableView详解(转)
首先.对UITableView进行讲解,下面有对它进行实际的应用 UITableView 显示大型内容的列表 单行,多列 垂直滚动,没有水平滚动 大量的数据集 性能强大,而且普遍存在于iPhone的应 ...
- UITableView详解(2)
承接上文,再续本文 一,首先我们对上次的代码进行改进,需要知道的一点是,滚动视图的时候,我们要创建几个视图,如果一个视图显示一个图片占据整个屏幕,对于滚动视图我们只需要创建三个视图就可以显示几千给图片 ...
- UITableView 详解 教程
看TableView的资料其实已经蛮久了,一直想写点儿东西,却总是因为各种原因拖延,今天晚上有时间静下心来记录一些最近学习的TableView的知识.下面进入正题,UITableView堪称UIKit ...
- UITableView详解
一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(, , , )]; [DataTable setD ...
- 《iOS 7 应用开发实战详解》
<iOS 7 应用开发实战详解> 基本信息 作者: 朱元波 管蕾 出版社:人民邮电出版社 ISBN:9787115343697 上架时间:2014-4-25 出版日期:2014 年5 ...
- iPhone应用开发 UITableView学习点滴详解
iPhone应用开发 UITableView学习点滴详解是本文要介绍的内容,内容不多,主要是以代码实现UITableView的学习点滴,我们来看内容. -.建立 UITableView DataTab ...
- IOS中表视图(UITableView)使用详解
IOS中UITableView使用总结 一.初始化方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styl ...
随机推荐
- .net framework 源码调试 与 问题解决
调试方式有二种, 看官方资料就OK. 官方地址: http://referencesource.microsoft.com/serversetup.aspx 1. 使用配置在线地址安装 2. 下载安装 ...
- PAT-乙级-1050. 螺旋矩阵(25)
1050. 螺旋矩阵(25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求将给定的N个正整数按非递增的 ...
- String 内在分配解析
1.String类概念 (1)String是final的,不可被继承.public final class String.String是的本质是字符数组char[], 并且其值不可改变.private ...
- android.os.DeadObjectException memory near r0: 异常处理 Consumer closed input channel or an error occurred. events=0x9
原地址:http://www.cnblogs.com/wanqieddy/p/3495338.html android.os.DeadObjectException memory near r0: 异 ...
- LR_问题_控制器不能使用定义的负载生成器
问题描述 在controller 中设置了面向目标的方案后 执行提示 The target you defined cannot be reached.the LoadRunner Controlle ...
- 10 harsh truths that will help you grow
10 harsh truths that will help you grow帮你成长的10个残酷事实In the game of life, if it often seems like you’r ...
- iOS开发--数组
1.sortedArrayUsingSelector (按Key值大小对NSDictionary排序) NSMutableArray *array = [NSMutableArray arrayWit ...
- org.json和json-lib比较
经常会用到JSON格式才处理,尤其是在Http请求的时候,网上可以找到很多json处理的相关工具,如org.json和json-lib,下面两段源代码是分别使用这两个工具解析和构造JSON的演示程序. ...
- Java API —— HashMap类 & LinkedHashMap类
1.HashMap类 1)HashMap类概述 键是哈希表结构,可以保证键的唯一性 2)HashMap案例 HashMap<String,String> ...
- 一个zip压缩类,欢迎吐槽
package com.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import j ...