iOS tableViewCell复用问题
来源:互联网 发布:国税务网络学校 编辑:程序博客网 时间:2024/05/21 09:12
当你上下来回滑动tableview的时候就会看到区别,第一种程序界面不会出现异常,但是第二种就不是了,会出现字体叠加现象,其实更确切的是多个label的叠加。为什么呢,因为在tableview刷新的时候,如果那个位置已经有现成的cell,它就不会再重新请求资源生成新的cell了,而是复用原来的cell。所以对于对于第一种,代码的思路是第一次在cell不存在的时候生成cell,定义cell样式,以后不管是刷新还是重新请求还好,它都只是复用已生成的cell。而第二种思路是,在cell不存在的时候,请求生成cell,然后给cell上添加label,刷新的时候,会复用已有的cell,但是会重复添加label,故造成重叠的现象。
//例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;}
4 0
- iOS tableViewCell复用问题
- 解决tableviewCell覆盖的问题以及tableviewCell复用原理
- [iOS 自定义TableviewCell 按钮复用问题] 按钮选择 避免复用
- 自定义tableViewCell 的复用问题
- iOS tableViewcell 里面含有uitextFiled 的问题
- TableViewCell 复用解决
- 解决tableViewCell的复用问题的方法
- tableViewCell的复用方法
- TableViewCell注册及复用
- iOS之ios8中TableViewCell中的短线不能居中问题
- ios之TableViewCell重用机制避免重复显示问题
- ios之TableViewCell重用机制避免重复显示问题
- ios之TableViewCell重用机制避免重复显示问题
- ios学习笔记:tableviewcell中ui设置frame问题
- ios之TableViewCell重用机制避免重复显示问题
- IOS storyboard解决TableViewCell底线无法到头问题
- ios之TableViewCell重用机制避免重复显示问题
- ios之TableViewCell重用机制避免重复显示问题
- Iterable_generator
- PHP通过kindeditor上传文件失败的扩展思考。
- NOIP2010提高组 关押罪犯
- Eclipse搭建android环境
- Android开发相关的Blog推荐
- iOS tableViewCell复用问题
- microstation vba起步——建立实体
- SpringMVC 配置文件
- Android面试题1
- Servlet 的生命周期 和 工作原理
- 204. Count Primes
- 2003 - Can connect to MySQL server on localhost (10038)mysql 读取描述文件失败 错误代码:2【亲测可用】
- Java构造器
- 微信公众平台开发[1] —— 获取用户Openid及个人信息