ios - 动画时透明的 UITableViewCell 闪烁背景

标签 ios objective-c uitableview ios7

我有一个带有自定义背景颜色的 UIViewController。在它的顶部有一个 UITableViewUITableViewCells 是半透明的(白色,不透明度为 0.5)。

我要指责的问​​题和我用头撞墙的问题是在 iOS 7 中,当你有一个具有半透明背景的 UITableViewCell 并且你尝试删除/插入/移动行(因此依赖于动画效果)整个 UITableView 及其单元格闪烁 仅 0.1 秒并将单元格背景设置为更透明的一个。这很烦人。

我唯一要做的就是设置 self.view 的背景颜色:

self.view.backgroundColor = [UIColor colorWithRed:0.4 green:0.5 blue:0.7 alpha:1];

并设置单元格的背景颜色:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
}

这是向您展示问题的 gif:

enter image description here

这是 super 简单的项目:https://github.com/socksz/TransparentCellFlashing

请帮我解决这个荒谬的问题! :P

最佳答案

  1. 创建UITableViewCell的子类(如命名为:CustomCell),并覆盖CustomCell.m中的方法:

    - (id)initWithCoder:(NSCoder *)aDecoder{
        self = [super initWithCoder:aDecoder];
        if(self){
            // Initialization code
            [self setBackgroundColor:[UIColor clearColor]];
            UIView * bgView = [[UIView alloc] initWithFrame:self.frame];
            [bgView setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.5]];
            [self addSubview:bgView];
        }
        return self;
    }
    
  2. 移除MasterViewController.m中的willDisplayCell方法

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
    }
    
  3. 修改MasterViewController.m中的cellForRowAtIndexPath方法

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        [self configureCell:cell atIndexPath:indexPath];
        return cell;
    }
    
  4. 在 Storyboard中更改单元格的类类型 enter image description here

试试吧:D

enter image description here

关于ios - 动画时透明的 UITableViewCell 闪烁背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459376/

相关文章:

objective-c - 跟踪 UIView 中的更改

iPhone:如何获取 iPhone 上所有地址簿的名称?

ios - 如何在 Xcode lldb 控制台窗口中显示 UITableView indexPath.row

ios - NSURLSession后台上传超大文件

iOS - 为什么使用 quick and nimble vs XCTest

html - iOS 上的 Bootstrap CSS 问题

iphone - uitableview 区分大小写的部分

iOS:在用户向下拖动时拉伸(stretch)/调整 UITableView 标题?

json - UITableView 不显示来自 URL 的图像

iOS 在 UITableViewCell 上获取 subview 的引用