[英]UITableView redraw issue on scrolling
我有一個從列表填充的分組UITableView
。
在某些行上,我不想公開,在某些行上,我需要添加標簽。
但是是否以某種方式混合並在錯誤的行上添加標簽並在每行顯示披露。
我在這里做錯了什么?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:@"customdisclosure.png" ]];
}
NSDictionary *dictionary = [_list objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Items"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
if([cell.textLabel.text isEqualToString:@"with label"])
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.text = @"label...";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else if([cell.textLabel.text isEqualToString: @"No disclosure" ])
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
在else if
子句中,您不會清除重用單元格上的cell.detailTextLabel
的文本。 將其設置為nil,就可以了。
cell.detailTextLabel.text = nil;
您還需要在else if
子句中隱藏accessoryView
視圖,然后取消隱藏它。
cell.accessoryView.hidden = YES;
總的來說,我會考慮對UITableViewCell
子類化,以便您可以重寫prepareForReuse
來為下一個cellForRowAtIndexPath
調用重置單元格。
猜猜問題出在使用可重用的標識符。 對不需要的附件視圖使用不同的單元格標識符。
static NSString *CellWithDisclosure = @"CellID_WithDisclosure";
static NSString *CellWithNoDisclosure = @"CellID_NoDisclosure";
NSDictionary *dictionary = [_list objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Items"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
if([cell.textLabel.text isEqualToString:@"with label"])
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithDisclosure];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else if([cell.textLabel.text isEqualToString: @"No disclosure" ])
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithNoDisclosure];
cell.accessoryType = UITableViewCellAccessoryNone;
}
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID] autorelease];
}
cell.textLabel.text = cellValue;
return cell;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.