UITableViewCell.accessoryView исчезнет в iOS 7

Я назначаю какое-то пользовательское представление для 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;
}

person Lings    schedule 02.01.2014    source источник


Ответы (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;
}
person NANNAV    schedule 02.01.2014
comment
Спасибо за ваш ответ, но этот код не работает в iOS7 (Xcode5+iOS7 SDK+ iOS7 Simulator/iPhone), когда я перетаскиваю таблицу с большим смещением, некоторые accessroyView исчезают, как и раньше :( - person Lings; 02.01.2014
comment
[dict objectForKey:@switch] — это просмотр? добавить код аксессуара. - person NANNAV; 02.01.2014
comment
создать пользовательский вид uiswitch, когда ячейка == nil, ячейка не использует один переключатель во всех представлениях - person NANNAV; 02.01.2014