在向下滚动然后备份之后,UITableViewCells会发生变化

我有一个UITableView,当我向下滚动和备份时,值在单元格的UILabels中变化。 我无法弄清楚为什么会发生这种情况。

我使用自定义的UITableViewCells,我自己subclassed。

这就是我的代码看起来像减去input到标签的所有值,因为我认为错误是不存在,但以我声明tableview委托方法或类似的东西大声笑。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. if (sortedItemsArray == nil) { return 0; } else return [sortedItemsArray count]; } - (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 *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; CustomInstallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[CustomInstallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; UILabel *lbl_name = [[UILabel alloc]initWithFrame:CGRectMake(10, 7, 280, 25)]; lbl_name.tag = 1000; [lbl_name setFont:[UIFont fontWithName:@"Helvetica" size:16.0f]]; lbl_name.lineBreakMode = UILineBreakModeWordWrap; lbl_name.numberOfLines = 0; [lbl_name setBackgroundColor:[UIColor clearColor]]; [lbl_name sizeToFit]; [cell.contentView addSubview:lbl_name]; //Create all your Labels here and set the tag } //Access your lbl by the tag here UILabel *lbl_name = (UILabel *)[cell.contentView viewWithTag:1000]; [lbl_name setText:[yourArray objectAtIndex:indexPath.row]]; cell.selectionStyle = UITableViewCellSelectionStyleGray; return cell; } 

像这样尝试..然后不需要检查数组cellForRowAtIndexPath中有元素:如果它有元素,那么只有循环来到这里。 我希望这对你有帮助

在“填写所有标签”的地方,我不确定你想要达到什么目的。

如果在你的自定义单元格中有很多UILabel使用sortedItemsArray填充,那么在- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section你不应该返回[sortedItemsArray count]

如果您的自定义单元格中有一个UILabel,并且您尝试使用基于索引的sortedItemsArray来填充标签,那么您所要做的就是

cell.yourCustomLabel = sortedItemsArray [indexPath.row];

如果您的情况不属于上述情况,请附上更多代码进行详细说明。

如果你有子类的uitableview单元格,请确保你在自定义单元格xib的atttrib utes检查器中给了“CellIdentifier”。 并像这样在indexpath处改变你的单元格

静态NSString * cellIdentifier = @“cell”;

 CustomInstallCell * cell = (CustomInstallCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomInstallCell" owner:self options:nil]; cell = [topLevelObjects objectAtIndex:0]; } 

UITableViewController使用可重用的单元格。 控制器创build有限数量的单元格,滚动只是覆盖内容。 这可能会导致单元格中某些属性的重复。 为避免重复,可以使用UITableViewDelegate的tableView:willDisplayCell:forRowAtIndexPath:方法。 在这种方法中,检查什么是重复或不正确,并将其设置正确。

UITableViewCell在滚动表格视图时重用单元格,基于CellIdentifier重用单元格,在表格视图中我们创build新的视图只有cell==nil其他明智的我们在表格视图中重用单元格,

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; CustomInstallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[CustomInstallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } if ([sortedItemsArray count] > 0) { UIView *view=[CustomInstallCell viewWithTag:your view tag]; //view is sub view of CustomInstallCell } } return cell; } 
 static NSString *CellIdentifier; if(IPAD) CellIdentifier=[NSString stringWithFormat:@"EditCell_ipad%d",indexPath.row]; else CellIdentifier=[NSString stringWithFormat:@"EditCell_iphone%d",indexPath.row]; CustomallCell *cell=(CustomallCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomallCell_TablviewCell" owner:self options:nil]; if(IPAD) cell = (CustomallCell*)[nib objectAtIndex:0]; else cell = (CustomallCell *)[nib objectAtIndex:1]; } 

我为每个单元格使用独特的Idetifier,所以没有机会重复/覆盖数据单元格的问题。像滚动之前的单元格状态改变,这将不会发生这种代码

 NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];