iOS UItableviewCell 清除旧的单元格值

标签 ios objective-c uitableview

首先很抱歉问这个问题,因为我知道有很多与此类似的问题。但到目前为止,没有什么对我有用。我有一个带有两个按钮的表格 View 。单击按钮时,它会加载两个不同的自定义单元格。但无论我尝试什么,我似乎都无法清除旧的单元格值。我已尝试使用 prepareForReuse 方法清除旧 View ,但最终单元格上没有显示任何内容。

这是我正在使用的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if([[self.sections objectAtIndex:indexPath.section] count] > 0)
        {
            id object = [[self.sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
            if (indexPath.section == self.sections.count-1)
            {
                if(self.CategoryButtonType == 0)
                {
                    MediaListCarousel * cell = [self getMediaCellCarousel:object];
                        return cell;
                }
                else
                {
                     NonMediaTableViewCell * cell = [self getNonMediaCell:object];
                      return cell;
                }
            }
            else 
            {
                 //Do other stuff
            }
        }
        return nil;
    }

这是自定义单元格

-(NonMediaTableViewCell *)getNonMediaCell:(NSString *)name
{
    NonMediaTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:nonmediaCellIdentifier];
    if (cell == nil) {
        cell = (NonMediaTableViewCell*)[VFHelper findCellWithClassName:[NonMediaTableViewCell class] nibName:@"NonMediaTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setNonMediaWithPackageName:name
                                         delegate:self];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


-(MediaListCarousel *)getMediaCellCarousel:(NSString *)name
{
    MediaListCarousel * cell = [self.tableView dequeueReusableCellWithIdentifier:mediaCellIdentifier];
    if (cell == nil) {
        cell = (MediaListCarousel*)[VFHelper findCellWithClassName:[MediaListCarousel class] nibName:@"MediaListCarousel"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setUp:name delegate:self];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

每个自定义单元格都具有如下定义的 prepareForReuse 方法。

- (void)prepareForReuse {
    [super prepareForReuse];
    // Clear contentView
    BOOL hasContentView = [self.subviews containsObject:self.contentView];
    if (hasContentView) {
        for(UIView *subview in [self.contentView subviews])
        {
            [subview removeFromSuperview];
        }
    }
}

当我在 prepareForReuse 方法中添加 removeFromSuperview 时发生了什么当我在两个 View 之间切换时我可以清空 View 。如果我不使用 prepareForReuse 来删除 View ,那么当我单击不同的按钮时,我最终会看到彼此重叠的 View 。有人可以帮忙解决一下吗?提前致谢。

编辑以显示自定义单元格代码

-(void) getNonMediaCell:(NSString *)package
                                      delegate:(NSObject <nonMediaCellDelegate> *)delegate
{
    self.package = package;
    [self loadValuefFromDB];
    self.addonName = addonName;

    _delegate = delegate;
    [self.btnSeemore addTarget:self action:@selector(btnSeemoreClicked) forControlEvents:UIControlEventTouchUpInside];
    if(_addons.isHidden)
    {
        _planDescTextView.hidden = YES;
        _heightConstraintforPlanDescView.constant = 0;
        _btnSeemore.imageView.transform = CGAffineTransformIdentity;
    }
    else
    {
        _planDescTextView.text = description;
        CGSize neededSize = [_planDescTextView sizeThatFits:CGSizeMake(_planDescTextView.frame.size.width, CGFLOAT_MAX)];
        _planDescTextView.hidden = NO;
        _heightConstraintforPlanDescView.constant = neededSize.height + 5;
        _btnSeemore.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        _btnSeemore.imageView.clipsToBounds = NO;
        _btnSeemore.imageView.contentMode = UIViewContentModeCenter;
    }

    if(IsEmpty(addons.state))
    {
        _UIViewEntitlement.hidden = YES;
        _UIViewBtnRepurchase.hidden = YES;
        _seperatorView.hidden = YES;
        [self resetCell];
        [self setPriceLabelValue];
        [self.purchaseBtn addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
        self.purchaseBtn.accessibilityLabel = [NSString stringWithFormat:@"%@_Purcahse", addonName];
        self.purchaseBtn.accessibilityValue = [NSString stringWithFormat:@"%@_Purcahse", addonName];
        self.purchaseBtn.accessibilityIdentifier = [NSString stringWithFormat:@"%@_Purcahse", addonName];
        self.recurringIndicatorImageView.hidden = NO;
        [self addOnRecurringIndicatorImages];
    }
    else
    {
        _UIViewEntitlement.hidden = NO;
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 20)];
        VFAddonPlanentitlementCell * cell = (VFAddonPlanentitlementCell*)[VFHelper findCellWithClassName:[VFAddonPlanentitlementCell class] nibName:@"VFAddonPlanentitlementCell"];
        [cell setFrame:CGRectMake(0, 0, self.frame.size.width, 20)];

        if(!IsEmpty(_addons.remainingDays))
        {
            if(recurring || allowCancel)
            {
                if([[_addons.state uppercaseString] isEqualToString:@"ONHOLD"])
                {
                    [cell setDaysRemainingColorWithTitleOnHold:@"Renews" daysRemainings:[_addons.remainingDays stringValue]];
                }
                else
                {
                    [cell setRemainingColorWithTitle:@"Renews" daysRemainings:[_addons.remainingDays stringValue]];
                }
            }
            else
            {
                [cell setDaysRemainingColorWithTitle:@"Time left" daysRemainings:[_addons.remainingDays stringValue]];
            }
            [headerView addSubview:cell];
        }
        [_UIViewEntitlement addSubview:headerView];
    }
}

最佳答案

试试这个

-(void) getNonMediaCell:(NSString *)package
                                      delegate:(NSObject <nonMediaCellDelegate> *)delegate
{
    .....//other code
    UIView *headerView = [_UIViewEntitlement viewWithTag:12345];
    if(headerView == nil) {
        headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 20)];
        [headerView setTag:12345];//set tag
    } else {
         NSLog(@"headerView already exist");
    }
    //set values
    .....//other code
}

以及重用方法

- (void)prepareForReuse {
    [super prepareForReuse];
    UIView *header = [_UIViewEntitlement viewWithTag:12345];//use same tag
    [header removeFromSuperview];
}

如果您要以编程方式创建和添加多个 View ,请为每个 View 设置标签,并通过使用相同的标签值访问它来删除重复使用的 View 。

关于iOS UItableviewCell 清除旧的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45505297/

相关文章:

ios - textView 在 tableViewCell 中显示时调整大小

ios - UILabel 没有显示更新后的高度?

ios - 简单的 UITableView 显示奇怪的数据顺序

ios - 无法将类型 'Option' 的值转换为预期的参数类型 '@noescape (Option) throws -> Bool'

ios - 带有下拉按钮的 UITextField

ios - 将coreplot图表点转换成superview的坐标空间

ios - 有没有办法以编程方式将 View 从一个按钮居中到另一个按钮?

ios - TableView 上下滚动几次后变慢

ios - 将点击事件添加到 IOS NSMutableAttributedString

ios - 自定义推送动画打破默认交互式弹出动画