我有一个带有自定义背景颜色的 UIViewController
。在它的顶部有一个 UITableView
和 UITableViewCells
是半透明的(白色,不透明度为 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:
这是 super 简单的项目:https://github.com/socksz/TransparentCellFlashing
请帮我解决这个荒谬的问题! :P
最佳答案
创建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; }
移除MasterViewController.m中的willDisplayCell方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5]; }
修改MasterViewController.m中的
cellForRowAtIndexPath
方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; [self configureCell:cell atIndexPath:indexPath]; return cell; }
在 Storyboard中更改单元格的类类型
试试吧:D
关于ios - 动画时透明的 UITableViewCell 闪烁背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459376/