简体   繁体   English

UITableView重绘滚动问题

[英]UITableView redraw issue on scrolling

I have a grouped UITableView that I populate from a list. 我有一个从列表填充的分组UITableView
On some rows I don't want to have disclosure and on some I need to add label. 在某些行上,我不想公开,在某些行上,我需要添加标签。
But is somehow mix something and add labels on wrong rows and display disclosures at each row. 但是是否以某种方式混合并在错误的行上添加标签并在每行显示披露。
What am I doing wrong here? 我在这里做错了什么?

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

in your else if clause, you are not clearing the cell.detailTextLabel 's text on the reused cell. else if子句中,您不会清除重用单元格上的cell.detailTextLabel的文本。 Set it to nil and you will be fine. 将其设置为nil,就可以了。

cell.detailTextLabel.text = nil;

You will also need to hide the accessoryView in the else if clause, and unhide it. 您还需要在else if子句中隐藏accessoryView视图,然后取消隐藏它。

cell.accessoryView.hidden = YES;

Overall, I would consider subclassing UITableViewCell so you can override prepareForReuse to reset your cell for the next cellForRowAtIndexPath call. 总的来说,我会考虑对UITableViewCell子类化,以便您可以重写prepareForReuse来为下一个cellForRowAtIndexPath调用重置单元格。

Guess the issue is with using reusable identifiers. 猜猜问题出在使用可重用的标识符。 Use different cell identifiers for the cells you do not want accessoryView. 对不需要的附件视图使用不同的单元格标识符。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM