1

我给了一些自定义视图UITableViewCell.accessoryView,但是如果我疯狂滚动tableView,一些accessoryView在iOS 7中会消失,如果我触摸单元格,它accessoryView会出现,我不知道为什么,因为它在iOS 6中是正确的。这是我的代码,有人可以帮助我吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"CELL %d", (int)indexPath.row+1];

    NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
    if ([dict objectForKey:@"switch"])
    {
        cell.accessoryView = [dict objectForKey:@"switch"];
    }
    else
    {
        cell.accessoryView = nil;
    }  

    return cell;
}
4

1 回答 1

1

当我们在表格视图中使用 ReusableCellWithIdentifier 时,它会重用表格中的单元格,您设置cell.accessoryView = nil; 它删除了具有相同CellIdentifier的表视图中所有单元格的附件视图尝试使用此代码,它可以解决您的问题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryView = [dict objectForKey:@"switch"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"CELL %d", (int)indexPath.row+1];

    if ([dict objectForKey:@"switch"])
    {
        cell.accessoryView.hidden=NO;
    }
    else
    {
        cell.accessoryView.hidden=YES;
    }  
    return cell;
}
于 2014-01-02T04:39:17.597 回答