我有一个UITableView,当我向下滚动和向上滚动时,单元格的UILabels中的值会发生变化。我不知道为什么会发生这种情况。
我使用的是我自己子类化的自定义UITableViewCells。
这就是我的代码看起来的样子,减去将所有值输入到标签中,因为我认为错误不存在,而是在我声明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;
}
我怎样才能停止细胞的重复?
7条答案
按热度按时间at0kjp5o1#
这样试试..那么就不需要检查数组cellForRowAtIndexPath中有元素了:如果它有元素,那么只有循环来这里。我希望这将有助于你
syqv5f0l2#
在你“填写所有标签”的地方,我不确定你想达到什么目的。
如果在你的自定义单元格中你有很多UILabel要用
sortedItemsArray
填充,那么在- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
中你不应该返回[sortedItemsArray count]
。如果自定义单元格中有一个UILabel,并且您尝试使用
sortedItemsArray
基于索引填充该标签,那么您需要做的就是第一步:创建一个新的数组;
如果你的情况不是以上任何一种,请附上更多的代码来详细说明。
j91ykkif3#
如果你已经子类化了uitableview单元格,确保你在你的自定义单元格xib的属性检查器中给出了“单元格标识符”,并像这样在indexpath处更改你的单元格行
静态NSString * 单元标识符= @“单元”;
jum4pzuy4#
UITableViewController使用可重复使用的单元格。该控制器创建有限数量的单元格,滚动只是覆盖内容。这可能导致单元格中某些属性的重复。为了避免重复,可以使用UITableViewDelegate的
tableView:willDisplayCell:forRowAtIndexPath:
方法。在此方法中,检查重复或不正确的内容,并正确设置。k10s72fa5#
UITableViewCell
重复使用单元格当滚动表格视图时,单元格基于CellIdentifier
重复使用,在表格视图中,我们仅创建新视图cell==nil
,否则我们在表格视图中重复使用单元格,3okqufwl6#
I am using unique Idetifier for each cell, so there is no chance to repeat/overwrite data cell problem.Like after scrolling the previous status of cell is changed that will not happen with this code
efzxgjgh7#