0

我已经对 a 进行了子类化,UITableViewCell并且这样做可以获得非常好的性能提升,但是我遇到了一个问题。我注意到当我向下滚动然后备份时我tableviewcells正在改变。

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

无论如何,我想向您展示我的tableviewdelegate方法,我使用tableView:cellForRowAtIndexPath:.

这是我的代码:

- (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];
}
4

1 回答 1

4

这个问题很常见,这与 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 中,清理单元格。当要重用单元格时,将调用该方法。

于 2013-10-31T08:52:45.377 回答