首页 技术 正文
技术 2022年11月14日
0 收藏 395 点赞 4,825 浏览 2850 个字

表刷新超出页面显示的内容会重复出现

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

//定义唯一标识

static NSString *cellId = @”Cell”;

//通过唯一标识创建cell实例

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

//判断为空进行初始化 — (刷动页面超出屏幕的时候就会重用之前的cell,而不会再次初始化)

if(!cell){

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];

}

cell.textLabel.text = @”你好!欢迎你”;

return  cell;

}

以下三个方法解决:

1.取消cell的重用机制,通过indexPath来创建cell将可以解决重复出现问题,不过这样做对于大数据来说内存就比较紧张

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

//定义唯一标识

static NSString *cellId = @”Cell”;

//通过indexPath创建cell实例,每个cell都是单独的

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//判断为空进行初始化(刷动页面超出屏幕的时候就会重用之前的cell,而不会再次初始化)

if(!cell){

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];

}

cell.textLabel.text = @”你好!欢迎你”;

return  cell;

}

2.让每个cell都拥有一个对应的标识 这样做也会让 cell无法重用 所以也就不会重复出现啦  显示内容比较多时内存占用也是比较多的和方法一类似

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

//定义cell 标识  每个cell对应一个自己的标识

static NSString *cellId = [NSString stringWithFormat:@”cell%ld%ld”,indexPath.section,indexPath.row];

//通过不同标识创建cell实例

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//判断为空进行初始化(刷动页面超出屏幕的时候就会重用之前的cell,而不会再次初始化)

if(!cell){

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];

}

cell.textLabel.text = @”你好!欢迎你”;

return  cell;

}

3. 只要最后一个显示的cell内容不为空,然后把它的子视图全部删除,等同于把这个cell单独出来了,然后刷新数据就可以解决重复显示问题

刷新需要显示新数据的时候,把最后一个cell进行删除,就有可以自定义cell,此方案既可避免重复出现,又重用了cell相对内存管理来说最好的方案,前两者相比比较消耗内存

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

//定义唯一标识

static NSString *cellId = @”Cell”;

//通过唯一标识创建cell实例

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

//判断为空进行初始化(刷动页面超出屏幕的时候就会重用之前的cell,而不会再次初始化)

if(!cell){

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];

}

else//当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们再进行数据配置即可避免

{

while ([cell.contentView.subviews lastObject] != nil) {

[(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];

}

}

cell.textLabel.text = @”你好!欢迎你”;

return  cell;

}

*******************多点了解*************************8

– (UITableViewCell  *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     static  NSString  *CellIdentiferId = @”MomentsViewControllerCellID”;
     UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
     if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@”MomentsCell” owner:nil options:nil];
        cell = [nibs lastObject];
        cell.backgroundColor = [UIColor clearColor];

};
        
     }
     return cell;

}严重注意:我们之前这么用都没注意过重用的问题,这样写,如果在xib页面没有设置 重用字符串的话,是不能够被重用的,也就是每次都会重新创建,这是严重浪费内存的

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,499
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,913
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,746
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,503
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,142
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,305