IOS UITabelView的cell
一、Cell的重用原理
iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象
重用原理:当 滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待 重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未 使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView, 重新显示到窗口中,从而避免创建新对象
还有一个非常重要的问题:有时候需要 自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种 UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的 UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的 UITableViewCell
解决方案:UITableViewCell 有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置 reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回 UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传 入这个字符串标识来初始化一个UITableViewCell对象
{
// 1.定义一个cell的标识
static NSString *ID = @"mycell";
// 2.从缓存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果缓存池中没有cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 4.设置cell的属性...
return cell;
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.tag = 1;
label.lineBreakMode = UILineBreakModeWordWrap;
label.highlightedTextColor = [UIColor whiteColor];
label.numberOfLines = 0;
label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[label release];
}
UILabel *label = (UILabel *)[cell viewWithTag:1];
NSString *text;
text = [textArray objectAtIndex:indexPath.row];
CGRect cellFrame = [cell frame];
cellFrame.origin = CGPointMake(0, 0);
label.text = text;
CGRect rect = CGRectInset(cellFrame, 2, 2);
label.frame = rect;
[label sizeToFit];
if (label.frame.size.height > 46) {
cellFrame.size.height = 50 + label.frame.size.height - 46;
}
else {
cellFrame.size.height = 50;
}
[cell setFrame:cellFrame];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
} 四、分割线
一般地,利用类似[tableView setSeparatorColor:[UIColor redColor]];语句即可修改cell中间分割线的颜色。那又如何用一个图片作为分割线背景呢?可以尝试如下:
方法一:
先设置cell separatorColor为clear,然后把图片做的分割线添加到自定义的custom cell上。
方法二:
在cell里添加一个像素的imageView后将图片载入进,之后设置tableView.separatorStyle = UITableViewCellSeparatorStyleNone
五、自定义首行Cell与其上面导航栏间距
tableView.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,5,20)] autorelease]; 六、cell触发
在自定义的btnClicked方法中判断出事件发生在UITableView的哪一个cell上。因为UITableView有一个很关键的方法indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个cell的indexPath。而且通过event对象,正好也可以获得每个触摸在视图中的位置。
// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
- (void)btnClicked:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if(indexPath != nil)
{
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
} 这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
int *idx = indexPath.row;
//这里加入自己的逻辑
}
此方法是理处每一个cell被择选后的动作
IOS UITabelView的cell的更多相关文章
- IOS Table中Cell的重用reuse机制分析
IOS Table中Cell的重用reuse机制分析 技术交流新QQ群:414971585 创建UITableViewController子类的实例后,IDE生成的代码中有如下段落: - (UITab ...
- iOS 点击cell下拉
iOS 点击cell下拉 代码如下: #import "ViewController.h" @interface ViewController ()<UITableView ...
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...
- ios中自定义cell 设置cell的分组结构
ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式 以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...
- iOS开发之cell多按钮
iOS开发经常出现cell需要多个按钮,一般以为要导入第三方框架.但其实iOS 8以后,系统提供了UITableViewRowAction以及新的delegate方法,使得自定义一些操作变得非常容易. ...
- iOS 8 自适应 Cell
在使用 table view 的时侯经常会遇到这样的需求:table view 的 cell 中的内容是动态的,导致在开发的时候不知道一个 cell 的高度具体是多少,所以需要提供一个计算 cell ...
- iOS中 自定义cell分割线/分割线偏移 韩俊强的博客
在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,并且只要上下分割线的一个等等需求,今天重点解决以上需求,仅供参考: 每日更新关注 ...
- Swift iOS tableView static cell动态计算高度
TableView是iOS开发中经常使用的组件.有些表格由于UILabel包括的文本字数不一样,须要显示的高度也会不同,因此须要动态计算static cell的高度.我用的是static cell,注 ...
- 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱
前言 API 不熟悉导致的问题,想当然的去理解果然会出问题,这里记录一下 UICollectionView 使用问题. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cn ...
随机推荐
- uva 10152 ShellSort
//这个算法用到了"相对位置"的思想,并且就本题而言还有一个很重要的结论就是,假设 //移动了k个元素,那么这k个元素一定是最后结果的那个序列的前k个元素,而且易知, //越先移动 ...
- cdoj 1255 斓少摘苹果 贪心
斓少摘苹果 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1255 Descr ...
- [Oracle] Group By 语句的扩展 - Rollup、Cube和Grouping Sets
常常写SQL语句的人应该知道Group by语句的主要使用方法是进行分类汇总,以下是一种它最常见的使用方法(依据部门.职位分别统计业绩): SELECT a.dname,b.job,SUM(b.sal ...
- decide your linux OS is GUI or not
Try: ps -ef|grep X The ps command will display information about a selection of the active process ...
- [Javascript] Manipulate the DOM with the classList API
Learn how to add, remove and test for CSS classes using the classList API. It's more powerful than u ...
- sizeWithFont 不是线程安全。
在ios开发中经常使用用sizeWithFont 方法来计算UILabel 的frame, 例如动态计算UITableViewCell 的高度,在主线程处理没有问题,但是在子线程用此方法来计算就会出现 ...
- Undefined property: Illuminate\Database\Eloquent\Builder
是因为在 $activity=Activity::where('center_id','=',$center->id)->where('Date','=',date("Y-m-d ...
- 小白日记21:kali渗透测试之提权(一)--本地提权
本地提权 简单地说,本地提权漏洞就是说一个本来非常低权限.受限制的用户,可以提升到系统至高无上的权限.权限提升漏洞通常是一种"辅助"性质的漏洞,当黑客已经通过某种手段进入了目标机器 ...
- BloomFilter——读数学之美札记
之前接触过bitmap,读吴军先生的数学之美,看到了一个更强大的数据结构,布隆过滤器(Bloomfilter),赶紧记下来吧,忘了怪可惜的. bitmap的使用是很有局限性的,往往只能用于海量数值型数 ...
- Windows主机通过SSH连接虚拟机里的Linux系统
Windows 7 + VMware 11 VMWare里:编辑-虚拟网络编辑器-VMnet8(NAT模式)-NAT设置...添加主机端口和虚拟机IP地址以及虚拟机端口 Windows7系统:wind ...