UITableView redraw issue on scrolling

本秂侑毒 提交于 2020-01-16 01:55:12

问题


I have a grouped UITableView that I populate from a list.
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;
}

回答1:


in your else if clause, you are not clearing the cell.detailTextLabel's text on the reused cell. Set it to nil and you will be fine.

cell.detailTextLabel.text = nil;

You will also need to hide the accessoryView in the else if clause, and unhide it.

cell.accessoryView.hidden = YES;

Overall, I would consider subclassing UITableViewCell so you can override prepareForReuse to reset your cell for the next cellForRowAtIndexPath call.




回答2:


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;


来源:https://stackoverflow.com/questions/18593059/uitableview-redraw-issue-on-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!