UITableView表格 UIlabel叠加 UIbutton点击 复用的问题
来源:互联网 发布:树洞外链美化源码 编辑:程序博客网 时间:2024/06/05 09:29
很多朋友觉得UITableViewCell复用问题很难处理,百思不得其解,甚至有很多朋友自己琢磨很久也不明白个究竟。现在分享一下个人的一些经验,希望对大家有帮助,如果有好的意见或者有不同的看法也可以提出来,让我们一起分享一起进步,知识只有在分享的情况下才能实现它的最大价值。好了,废话少说,直奔主题了。列举两个场景对比一下,也许tableviewcell的复用就很清晰明了了。 例1: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"cell1"; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UILabel *labelTest = [UILabel alloc]init]; [labelTest setFrame:CGRectMake(2, 2, 80, 40)]; [labelTest setBackgroundColor:[UIColor clearColor]; [labelTest setTag:1]; [cell contentView]addSubview:labelTest]; } UILabel *label1 = (UILabel*)[cell viewWithTag:1]; [label1 setText:[self.tests objectAtIndex:indexPath.row]; return cell; } 例2: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ static NSString *CellIdentifier = @"cell1"; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } UILabel *labelTest = [UILabel alloc]init]; [labelTest setFrame:CGRectMake(2, 2, 80, 40)]; [labelTest setBackgroundColor:[UIColor clearColor]; //之所以这里背景设为透明,就是为了后面让大家看到cell上叠加的label。 [labelTest setTag:1]; [cell contentView]addSubview:labelTest]; [labelTest setText:[self.tests objectAtIndex:indexPath.row]; return cell; } 当你上下来回滑动tableview的时候就会看到区别,第一种程序界面不会出现异常,但是第二种就不是了,会出现字体叠加现象,其实更确切的是多个label的叠加。为什么呢,因为在tableview刷新的时候,如果那个位置已经有现成的cell,它就不会再重新请求资源生成新的cell了,而是复用原来的cell。所以对于对于第一种,代码的思路是第一次在cell不存在的时候生成cell,定义cell样式,以后不管是刷新还是重新请求还好,它都只是复用已生成的cell。而第二种思路是,在cell不存在的时候,请求生成cell,然后给cell上添加label,刷新的时候,会复用已有的cell,但是会重复添加label,故造成重叠的现象。二 .UIbutton-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中进行获取这个tag值,获取方法是:
UIButton *exitBtn = (UIButton *)[cell viewWithTag:1];//1是我上面设置的tag值
这样就获取到了,你再给他添加action事件就可以了,
0 0
- UITableView表格 UIlabel叠加 UIbutton点击 复用的问题
- UIButton点击不灵敏的问题.
- UIButton 无法点击的问题
- jQuery点击事件的叠加问题
- UILabel和UIButton的属性
- 用runtime 解决UIButton 重复点击问题
- 用Runtime解决UIButton重复点击问题
- 用runtime 解决UIButton 重复点击问题
- UIButton 同时点击问题
- 解决UI摄像机叠加时的点击重叠问题
- UILabel UIButton 混合问题 -iOS初学
- UILabel、UIButton
- UIButton&UILabel
- UILabel UIButton
- UIButton 的点击事件
- UIButton的点击状态
- UIScrollview下面的UIButton不能点击的问题
- UILabel,UITextField 以及UIButton的应用
- android listview中放radiogroup选择混乱
- PAT-甲级-1001
- 用iis做的ipa下载网站,不能下载app求帮忙,说是描述文件的问题,求帮忙
- session共享
- iOS 在一个屏幕中根据不同的按钮创建不同的 view 默认显示第一个
- UITableView表格 UIlabel叠加 UIbutton点击 复用的问题
- 关于table view cell选择控制器后出错
- BZOJ 2631 tree LCT
- view.draw(canvas)之后view里的控件属性消失了?
- Xcode6 beta版使用问题:图片资源无法预览
- 启动应用显示LOGO再跳转页面出现问题
- BZOJ 2733 [HNOI2012] 永无乡 Treap
- Mybatis两种开发方式
- Android新手,xxx has stopped。希望有人帮我看一下日志提示的错误我应该去改哪。