TableView滚动时如何避免UITableViewCell重复添加自定义标签? [英] How to avoid UITableViewCell repeat add custom label when TableView scrolling?

查看:67
本文介绍了TableView滚动时如何避免UITableViewCell重复添加自定义标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义单元格:

I create a custom Cell :

#import <UIKit/UIKit.h>
#import "RecruitmentResumeEntity.h"

@interface RecruimentListItemCell : UITableViewCell
@property (nonatomic, strong) RecruitmentResumeEntity *entity;
@end

并且在 .m 文件中设置实体方法我添加了三个标签:

and In setting entity method in .m file I add three label:

-(void)setEntity:(RecruitmentResumeEntity *)entity {

   _entity = entity;

   float labelHeight = 17;

   CGRect frame = CGRectMake(64, 45, 0, labelHeight);
   UILabel *cityLabel = [self createTagLabelWithFrame:frame text:_entity.city backgroundColor:@"#e5986f"];
   [self.contentView addSubview:cityLabel];

   UILabel *workExperienceLabel = [self createTagLabelWithFrame:CGRectMake(cityLabel.x+cityLabel.width +10, cityLabel.y, 0, labelHeight) text:[NSString stringWithFormat:@"%@年",_entity.workExperience] backgroundColor:@"#81A0D7"];
   [self.contentView addSubview:workExperienceLabel];

   UILabel *expectSalaryLabel = [self createTagLabelWithFrame:CGRectMake(workExperienceLabel.x+workExperienceLabel.width +10, workExperienceLabel.y, 0, labelHeight) text:_entity.expectSalary backgroundColor:@"#94C373"];
   [self.contentView addSubview:expectSalaryLabel];
}

在控制器 cellForRowAtIndexPath 方法中获取自定义单元格并设置实体.但是当我运行应用程序,滚动 UITableView 时,我发现单元格重复创建了三个标签,我只希望每个单元格只有三个标签.我是不是弄错了什么或遗漏了什么.任何人都可以帮助我吗?等待您的帮助.谢谢.

In Controller cellForRowAtIndexPath method get custom cell and set entity. But when I run app,scrolling the UITableView, I found that the cell repeat create three label and I just want each cell only has three Label. Did I get something wrong or missing . Can anyone can help me ? Waiting for you help.Thanks.

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

   RecruitmentResumeEntity *entity = _dataList[indexPath.row];
   RecruimentListItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecruimentListItemCell" forIndexPath:indexPath];
   cell.entity = entity;

   return cell;
}

推荐答案

这似乎是您没有正确重新使用单元格的问题.

This seems to be a problem where you are not properly RE-using the cell.

当你调用 RecruimentListItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecruimentListItemCell" forIndexPath:indexPath]; 系统会给你一个它认为可以重复用于显示下一个信息的单元格它需要显示的单元格,但在您重新使用之前正确准备该单元格完全是您的责任.

When you call RecruimentListItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecruimentListItemCell" forIndexPath:indexPath]; the system hands you a cell that it thinks can be re-used to display information for the next cell that it needs to display, but it is solely your responsibility that this cell is properly prepared before you reuse it.

为此,您必须在 RecruimentListItemCell 中实现 -(void)prepareForReuse,在那里您必须确保所有需要重新填充的项目单元格被正确重置,以便何时可以正确地重新填充单元格.

For this purpose you will have to implement -(void)prepareForReuse in RecruimentListItemCell, in there you will have to make sure that all items that need to be repopulated in the cell are reset properly so that when the cell can be repopulated properly.

例如,如果在 RecruimentListItemCell 中添加标签SampleLable"作为子视图,但在 -(void)prepareForReuse 然后每次重复使用单元格时,SampleLable"将一次又一次地添加到其中,最终您会注意到事情看起来不像它们应该的那样,就像您的重复创建标签"一样

For example, if in RecruimentListItemCell you are adding a label "SampleLable" as a subview, but you do not do [SampleLable removeFromSuperView] in -(void)prepareForReuse then every time the cell is reused, "SampleLable" will be added to it again and again, eventually you will notice that things dont look as they should, as is the case with your "repeat create label"

这是 prepareForReuse 在我的一个应用中的样子:

This is how prepareForReuse looks in one of my apps:

-(void)prepareForReuse {

[super prepareForReuse];

[_statesView.activityIcon1 setHidden:YES];
[_statesView.activityIcon2 setHidden:YES];

_title.text = nil;
_infoText.text = nil;
_createdTime.text = nil;
_cellType = CustomCellTypeNone;

[_progressView setProgress:0];
}

在这里你可以看到我刚刚设置为 nil 的一些项目(并不总是最好的方法),如果需要,这些只是再次放入.你可以看到我隐藏了activityIcon 1和2.当下一个单元格出现时,如果它需要图标,它会简单地取消隐藏它们,如果它需要标签,那么它会添加它们.

Here you can see that some items I just set to nil (not always the best approach), if needed these are just put in again. You can see that I hide activityIcon 1 and 2. When the next cell comes up, if it needs the icons it simply unhides them, if it needs the labels then it adds them.

因此,如果我不将所有标签置零,则会发生它们留在视图中并且下一个单元格可能会添加自己的标签,这将导致您遇到的问题.

So if I dont nil all the labels, what happens is that they stay in the view and the next cell might add its own labels, this will lead to the problem that you are encountering.

这篇关于TableView滚动时如何避免UITableViewCell重复添加自定义标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆