如何在重用之前重置内容

如何在重用之前重置内容

本文介绍了UITableViewCell - 如何在重用之前重置内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个令人讨厌的错误,我无法修复。



我有一个 CustomCell ,并且在其中我有一个子视图,根据对象的值改变它的颜色。

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath :(NSIndexPath *)indexPath {
static NSString * cellIdentifier = @CustomCell;

CustomCell * cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

if(cell == nil){
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

MyObject * object = [self.fetchedResultsController objectAtIndexPath:indexPath];

if([object.redColor isEqualToNumber:[NSNumber numberWithBool:YES]]){
cell.colorView.backgroundColor = [UIColor redColor];
}
else {
cell.colorView.backgroundColor = [UIColor clearColor];
}

返回单元格;
}

除非我用<$ c $删除一行,否则这一切都正常从tableview中c> redColor = YES ,滚动显示不可见的行。变为可见的第一行(重用可重用单元格的第一行)具有红色,即使该行为 redColor = NO 。如果我再次滚动并隐藏单元格然后再次显示它,颜色将设置为clearColor,它应该是这样的。



我认为这是因为它正在重复使用刚被删除的单元格。
所以我试图在重用之前重置单元格的内容。
CustomCell.m

   - (void)prepareForReuse {
[super prepareForReuse];

self.clearsContextBeforeDrawing = YES;
self.contentView.clearsContextBeforeDrawing = YES;
self.colorView.backgroundColor = [UIColor clearColor];
}

但这不起作用。
Apple Doc说

重置内容的正确方法是什么?我是否必须从超视图中删除子视图?



提前致谢

解决方案

这似乎有效。



我在 CustomCell.m



- (void)prepareForReuse {
[super prepareForReuse];

//清除contentView
BOOL hasContentView = [self.subviews containsObject:self.contentView];
if(hasContentView){
[self.contentView removeFromSuperview];
}
}

中再次添加cellForRowAtIndexPath

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

// Cell
static NSString * cellIdentifier = @CustomCell;

CustomCell * cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

if(cell == nil){
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

//恢复contentView
BOOL hasContentView = [cell.subviews containsObject:cell.contentView];
if(!hasContentView){
[cell addSubview:cell.contentView];
}

//配置单元格
MyObject * object = [self.fetchedResultsController objectAtIndexPath:indexPath];

if([object.redColor isEqualToNumber:[NSNumber numberWithBool:YES]]){
cell.colorView.backgroundColor = [UIColor redColor];
}
else {
cell.colorView.backgroundColor = [UIColor clearColor];
}

返回单元格;
}

希望这会对某人有所帮助。


There's an annoying bug that I can't fix.

I have a CustomCell, and in it I have a subview that changes it's color according to object's value.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    MyObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];

    if ([object.redColor isEqualToNumber:[NSNumber numberWithBool:YES]]) {
        cell.colorView.backgroundColor = [UIColor redColor];
    }
    else {
        cell.colorView.backgroundColor = [UIColor clearColor];
    }

    return cell;
}

This is all working fine except when I delete a row with redColor = YES from the tableview, and I scroll to show rows that was not visible. The first row that becomes visible (first row that reuses the reusable cell) has red color, even though that row is redColor = NO. And if I scroll again and hide the cell and then show it again, color is set to clearColor, the way it should be.

I think this is because it's reusing the cell that has just been deleted.So I'm trying to reset cell's content before reusing.In CustomCell.m

- (void)prepareForReuse {
    [super prepareForReuse];

    self.clearsContextBeforeDrawing = YES;
    self.contentView.clearsContextBeforeDrawing = YES;
    self.colorView.backgroundColor = [UIColor clearColor];
}

But this is not working.Apple Doc says

What is the proper way to reset content? Do I have to remove subviews from the superview?

Thanks in advance

解决方案

This seems to work.

I remove the cell's contentView when prepareForReuse in CustomCell.m

- (void)prepareForReuse {
    [super prepareForReuse];

    // Clear contentView
    BOOL hasContentView = [self.subviews containsObject:self.contentView];
    if (hasContentView) {
        [self.contentView removeFromSuperview];
    }
}

Add it in again in cellForRowAtIndexPath

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

    // Cell
    static NSString *cellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    // Restore contentView
    BOOL hasContentView = [cell.subviews containsObject:cell.contentView];
    if (!hasContentView) {
        [cell addSubview:cell.contentView];
    }

    // Configure cell
    MyObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];

    if ([object.redColor isEqualToNumber:[NSNumber numberWithBool:YES]]) {
        cell.colorView.backgroundColor = [UIColor redColor];
    }
    else {
        cell.colorView.backgroundColor = [UIColor clearColor];
    }

    return cell;
}

Hope this will help someone.

这篇关于UITableViewCell - 如何在重用之前重置内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:09