ios - UITableViewCell.accessoryView 将在 iOS 7 中消失

标签 ios uitableview ios7 accessoryview

我将一些自定义 View 分配给 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;
}

最佳答案

当我们在 TableView 中使用 ReusableCellWithIdentifier 时,它会重用表中的单元格,您设置 cell.accessoryView = nil; 它会删除 TableView 中所有具有相同 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;
}

关于ios - UITableViewCell.accessoryView 将在 iOS 7 中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20876059/

相关文章:

ios - MFMailComposeViewController 在 iPad 上崩溃

ios - 要在此设备上获取测试广告,请调用 : request. testDevices = [NSArray arrayWithObjects :GAD_SIMULATOR_ID, nil];

ios - pod 安装失败, react native 0.44

ios - 我想在我的 iPhone 应用程序中使用多种货币选项作为金额,是否有可用的货币 api 或数据库文件?

objective-c - 如何检测行上单击按钮的indexPath?

ios - 如何快速从 Storyboard中的 didSelectRowAt indexPath 打开 View 选项卡 Controller ?

iphone - iOS使用NSDictionary将数据加载到节和行中

ios - 如何使用 Xcode51_Be​​ta 在 iOS 7.1 beta 设备中编译应用程序

iOS - 如何永久保存密码?

ipad - iOS 7's drawViewHierarchyInRect doesn' t 工作?