ios - UITableViewCell 的子类行为不正确

标签 ios objective-c uitableview

我已经对 UITableViewCell 进行了子类化,并且从中获得了非常好的性能提升,但是我遇到了一个问题。我注意到当我向下滚动然后向上滚动时,我的 tableviewcells 正在发生变化。

在仔细检查时,我认为当我向上滚动时,底部的值被添加到 tableview 的顶部。然而,这是我第一次对 UITableViewCell 进行子类化,所以我遇到了一些困难。

无论如何,我想向您展示我的 tableviewdelegate 方法,我使用 tableView:cellForRowAtIndexPath: 将自定义单元格添加到 tableview。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[CustomallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    if ([sortedItemsArray count] > 0) {
        currentallDictionary = [sortedItemsArray objectAtIndex:indexPath.row];
        
        NSNumber *tempDU = [currentallDictionary objectForKey:@"DU"];
        NSInteger myInteger = [tempDU integerValue];
        
        if (myInteger == 0) {

            //NSLog(@"%@", currentallDictionary);
            //assign vals to labels

            NSString *areaString = [currentallDictionary objectForKey:@"area"];
            if ((NSNull *) areaString != [NSNull null]) {
                cell.areaLabel.text = areaString;
            } else {
                cell.areaLabel.text = @" ";
            }
            

            NSString *stageString = [currentallDictionary objectForKey:@"stage"];
            if ((NSNull *) stageString != [NSNull null]) {
                cell.stageLabel.text = stageString;
            } else {
                cell.stageLabel.text = @" ";
            }
            

            NSString *floorLabelString = [currentallDictionary objectForKey:@"floorNo"];
            if ((NSNull *) floorLabelString != [NSNull null]) {
                cell.floorLabel.text = floorLabelString;
            } else  {
                cell.floorLabel.text = @" ";
            }
            

            NSString *floorDescLabelString = [currentallDictionary objectForKey:@"floorDesc"];
            if ((NSNull *) floorDescLabelString != [NSNull null]) {
                cell.floorDescLabel.text = floorDescLabelString;
            } else  {
                cell.floorDescLabel.text = @" ";
            }  
            
//Buttons
            tDxStateAString = [currentallDictionary objectForKey:@"tDxStateA"];
            tDxStateBString = [currentallDictionary objectForKey:@"tDxStateB"];
            tHasDxString = [currentallDictionary objectForKey:@"tHasDx"];
            
            if ([tHasDxString isEqualToString:@"T"]) {
                tDxQtyString = [currentallDictionary objectForKey:@"tDxQty"];
                if ((NSNull *) tDxQtyString != [NSNull null]) {
                    cell.quantityALabel.text = tDxQtyString;
                    
                    
                    if (([tDxStateAString isEqualToString:@"T"]) && ([tDxStateBString isEqualToString:@"W"])) {
                        UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
                        [cell.DxfitButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
                    } else if (([tDxStateAString isEqualToString:@"T"]) && ([tDxStateBString isEqualToString:@"R"])) {
                        UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
                        [cell.DxfitButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
                    }else {
                        UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
                        [cell.DxfitButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
                        cell.DxfitButtonImage.userInteractionEnabled = YES;
                    }
                    
                } else {
                    cell.quantityALabel.text = @" ";
                    cell.DxfitButtonImage.userInteractionEnabled = NO;
                }
            }

            tStateAString = [currentallDictionary objectForKey:@"tStateA"];
            tStateBString = [currentallDictionary objectForKey:@"tStateB"];
            tHasInsString = [currentallDictionary objectForKey:@"tHasIns"];
            if ([tHasInsString isEqualToString:@"T"]) {
                
                tInsQtyString = [currentallDictionary objectForKey:@"tInsQty"];
                if ((NSNull *) tInsQtyString != [NSNull null]) {
                    cell.quantityBLabel.text = tInsQtyString;
                    
                    if (([tStateAString isEqualToString:@"T"]) && ([tStateBString isEqualToString:@"W"])) {
                        UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
                        [cell.allButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
                    } else if (([tStateAString isEqualToString:@"T"]) && ([tStateBString isEqualToString:@"R"])) {
                        UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
                        [cell.allButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
                    } else {
                        UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
                        [cell.allButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
                    }
                } else {
                    cell.quantityBLabel.text = @" ";
                    cell.allButtonImage.userInteractionEnabled = NO;
                }
            }
            
            
            
        }
    }
    return cell;
}

更新

我已将此方法添加到我的自定义 UITableViewCell 类中,但是尽管问题没有那么严重,但我发现如果我将手指按住屏幕并在不松手的情况下上下缩放它,它似乎会摇动周围的单元格,不同的值像以前一样出现,但有点不同。也许这是我的代码的逻辑问题?

- (void)prepareForReuse {
    [super prepareForReuse];
    areaLabel = nil;
    stageLabel = nil;
    floorLabel = nil;
    floorDescLabel = nil;
    // etc....

    [self didTransitionToState:UITableViewCellStateDefaultMask];
}

最佳答案

这个问题很常见,它与 UITableView 重用您的单元格这一事实有关。

当您调用 dequeue cell 时,您正在获取从顶部消失的单元格并在底部重新使用它们。

您需要在重新使用之前“清理”一个单元格。

您可以在将其出队后删除其上的所有内容:

    CustomallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[CustomallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

//Remove or clean subviews here

或者,实现这个方法:

- (void)prepareForReuse

在 UITableViewCell 中,清理单元格。当要重新使用单元格时将调用该方法。

关于ios - UITableViewCell 的子类行为不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19701662/

相关文章:

ios - 由 UITapGestureRecognizer 创建的单击功能在 UIButton 上不起作用

ios - 导致自托管视频无法在移动设备上运行的幕后原因是什么

ios - 如何在通知操作中显示特定的 View Controller ?

objective-c - 您应该在使用委托(delegate)的类中还是在类本身中将委托(delegate)设置为 nil

iphone - 不同高度的NSCollectionView : How to do this on desktop?

ios - 如何在 iOS 中实现如下图所示的搜索功能?

objective-c - UITableViewCell 上 showingDeleteConfirmation 的奇怪行为

ios - 如何在观看之前准备内容?

objective-c - 使用 UIViewController 调用多个 ViewController

swift - 在 Swift 中提取并转换为字符串