【问题标题】:Transparent UITableViewCell flashing background when animating动画时透明UITableViewCell闪烁背景
【发布时间】:2013-12-08 21:31:42
【问题描述】:

我有一个带有自定义背景颜色的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:

这是一个超级简单的项目:https://github.com/socksz/TransparentCellFlashing

请帮我解决这个可笑的问题! :P

【问题讨论】:

  • 你的 UITableView 的背景色是什么?如果您设置了一个,请尝试在您的 xib/storyboard 中将其设置为“清除”(或您选择的任何颜色)。通常在 iOS7 中,它被设置为“默认”,这是大多数这些问题的原因。
  • 好像没关系。我尝试将self.view(即表格视图)的背景颜色设置为[UIColor clearColor],并尝试将表格视图嵌入UIView(视图控制器的主视图)并设置背景此视图的颜色要清除。没用。
  • 哦,好的。然后,您可能需要通过子类化 UITableViewCell 来覆盖 UITableViewCell 的 setSelected() 属性。或者至少使用 backgroundView 属性而不是 backgroundColor。

标签: ios objective-c uitableview ios7


【解决方案1】:
  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. 在情节提要中更改单元格的类类型

试一试:D

【讨论】:

  • 我爱你!它就像一个闪亮的魅力。为什么我们需要添加自定义视图作为背景视图?也许是因为UITableViewCellbackgroundViewbackgroundColor 属性以这种方式工作,而我们无法改变这种行为?谢啦! PS:GIF 也是 +1 :-)
  • 它在 iOS6 中运行良好,我们只设置单元格的 backgroundView 或 backgroundColor。实际上,我不太确定为什么在 iOS7 中更新时单元格会闪烁... :D
  • 嘿,我遇到了完全相同的问题,但我没有使用故事板。所以我可以在 initWithFrame:cellReuseIdentifier 中做 initWithCoder 的事情吗??
  • 如果我将Subview 添加到单元格,它不会出现在 textLabel 之上吗?因为我看不到我的 textLabel?
  • @user1010819 (1)在xib/storyboard加载视图时,框架会调用initWithCoder。不要手动调用initWithCoder! (2)如果看不到label,可以在addSubview:...之后调用[self.view sendSubviewToBack: bgView]...试试看!
猜你喜欢
  • 1970-01-01
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多