动画时透明的UITableViewCell闪烁背景 [英] Transparent UITableViewCell flashing background when animating
问题描述
我有一个带有自定义背景颜色的 UIViewController
。除此之外还有一个 UITableView
,其中 UITableViewCells
是半透明的(白色,不透明度为0.5)。
I have a UIViewController
with a custom background color. On top of it there's a UITableView
with UITableViewCells
that are semi-transparent (white color with opacity 0.5).
当我有一个 UITableViewCell时,我正在责备的问题和我正在撞墙的问题是在iOS 7中code>半透明背景,你尝试删除/插入/移动行(依赖于动画效果)整个
UITableView
及其单元格 闪烁 仅需0.1秒,并将单元格背景设置为更透明的背景。这非常烦人。
The issue I'm blaming about and the one I'm banging my head against the wall is in iOS 7, when you have a UITableViewCell
with semi-transparent background and you try to delete/insert/moving rows (so relying on an animation effect) the entire UITableView
with its cells flashing for just 0.1 second and set cells background to a more transparent one. This is very annoying.
我唯一要做的就是设置 self.view
的背景颜色:
The only thing I'm doing is set the background color of self.view
with:
self.view.backgroundColor = [UIColor colorWithRed:0.4 green:0.5 blue:0.7 alpha:1];
并设置单元格的背景颜色:
and set the background color of cells with:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
}
这是一个显示问题的gif:
Here's a gif showing you the problem:
这是一个超级简单的项目: https://github.com/socksz/TransparentCellFlashing
And here's the super simple project: https://github.com/socksz/TransparentCellFlashing
请帮我解决这个荒谬的问题! :P
Please help me to solve this ridicolous issue! :P
推荐答案
-
创建UITableViewCell的子类(如命名为:CustomCell),并覆盖CustomCell.m中的方法:
Create a subclass of UITableViewCell(Such as named: CustomCell), and override the method in 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方法
Remove willDisplayCell method in MasterViewController.m
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
}
更改 cellForRowAtIndexPath $ c MasterViewController.m中的$ c>方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
在故事板中更改单元格的类类型
Change the cell's class type in storyboard
试一试:D
这篇关于动画时透明的UITableViewCell闪烁背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!