我已经将UITableViewCell
子类化了,并且这样做的性能得到了很好的提升,但是我遇到了一个问题。我注意到当我向下滚动然后备份我的tableviewcells
正在改变。
仔细检查后,我认为当我向上滚动时,底部的值会被添加到tableview
的顶部。然而,这是我第一次将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];
}
答案 0 :(得分:4)
这个问题很常见,而且与UITableView重用你的单元格有关。
当你打电话给出队时,你正在从顶部消失并将它们放回到底部。
在重复使用之前,您需要“清理”一个单元格。
您可以在出列后将其上的所有内容删除:
CustomallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Remove or clean subviews here
或者,实现此方法:
- (void)prepareForReuse
在UITableViewCell中,清理单元格。当要重复使用该单元时,将调用该方法。