2013-12-08 101 views
6

我有一个UIViewController自定义背景颜色。最重要的是,UITableViewUITableViewCells是半透明的(白色,不透明度为0.5)。透明UITableViewCell动画时闪烁背景

我被指责的问题以及我将头撞到墙上的问题是在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

他重新的超简单的项目:https://github.com/socksz/TransparentCellFlashing

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

+0

什么是你的UITableView的背景颜色?如果您已经设置了一个,请尝试在xib/storyboard中将其设置为“清除”(或您选择的任何颜色)。通常在iOS7中,它被设置为“默认”,这是造成这些问题的主要原因。 –

+0

看来没关系。我试图将'self.view'(这是表视图)的背景颜色设置为'[UIColor clearColor]',并试图将表视图嵌入到'UIView'中(视图控制器的主视图),并设置该视图的背景颜色清除。不起作用。 –

+0

哦,好的。然后你可能需要通过继承你的UITableViewCell的setSelected()属性来覆盖它。或者至少使用backgroundView属性而不是backgroundColor。 –

回答

11
  1. 创建(如命名:CustomCell)的UITableViewCell的子类,并覆盖在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

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5]; 
    } 
    
    删除willDisplayCell方法
  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. 改变细胞的类型在故事板 enter image description here

只是有一个尝试:d

enter image description here

+1

我爱你!它像一个闪亮的魅力。为什么我们需要将自定义视图添加为背景视图?也许是因为'UITableViewCell'的'backgroundView'和'backgroundColor'属性是这样工作的,我们无法做任何事情来改变这种行为?谢啦! PS:对于GIF也为+1 –

+1

它在iOS6中正常工作,我们只设置cell的backgroundView或backgroundColor。实际上,我不太确定为什么在iOS7中更新时闪烁的单元格:D – Joiningss

+0

是的,它可能是iOS 7。不过,再次感谢你。 –