UITableViewCell中滑动删除有白色背景,需要清除
我试图更改滑动 UITableViewCell 行时出现的视图的背景颜色,即“删除”按钮后面的背景颜色。
我尝试更改 cell.editingAccessoryView 但这没有做任何事情。
UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
myBackgroundView.backgroundColor = [UIColor clearColor];
cell.editingAccessoryView = myBackgroundView;
有什么想法吗?
I am trying to change the background color of the view that appears when you swipe a UITableViewCell row, the background color behind the 'Delete' button.
I tried changing the cell.editingAccessoryView but that didn't do anything.
UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
myBackgroundView.backgroundColor = [UIColor clearColor];
cell.editingAccessoryView = myBackgroundView;
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我回答这个问题是因为我花了一段时间才找到答案,这是搜索中出现的第一个条目。通过使用
willDisplayCell
方法,您可以访问单元格背景颜色。请注意,[UIColor clearColor];
将返回白色,因此请相应地调整您的代码。I'm answering this because it took me a while to find an answer and this is the first entry that comes up in a search. By using the
willDisplayCell
method you can access the cells background color. Note that[UIColor clearColor];
will return white in so adjust your code accordingly.你快到了。
UITableViewCell
类有一个backgroundView
属性,默认情况下为nil
。只需按照您在问题中所做的那样创建一个新的UIView
,然后将其分配给单元格的backgroundView
属性即可。You're almost there. The
UITableViewCell
class has abackgroundView
property, which isnil
by default. Just create a newUIView
as you've done in your question, then assign that to thebackgroundView
property of your cell.我认为这取决于您如何将内容添加到单元格中。
当我使用 [cell addSubView] 或 [cell.contentView addSubView] 直接将内容添加到单元格时,我遇到了同样的问题。
我的解决方法是:
并且您不再需要篡改backgroundView属性。我已经尝试过这个并且效果很好!
I think it depends on how you are adding content into your cell.
When i added content to the cell directly using [cell addSubView] or [cell.contentView addSubView] i was having the same problem.
My workaround to this was:
And you do not need to tamper with the backgroundView property anymore. I have tried this and works perfectly!
虽然这些答案是正确的,但我认为在大多数情况下,在界面生成器中设置单元格的背景颜色会更容易。我的意思是单元格的实际背景颜色属性,而不是它的内容视图。如果它始终是内容视图的颜色,则没有理由动态地执行此操作。
While these answers are right I feel that for most cases it's easier just to set the cell's background color in interface builder. By that I mean the actual background color property of the cell, not it's content view. There's no reason to do it dynamically if it's always gonna be the color that the content view is anyway.
iOS8 为
UITableViewDelegate
添加了新方法:您可以创建可自定义的
UITableViewRowAction
来控制行操作。例如:
有关更多信息,请查看此处和此处
使用新 API 的好示例:此处
iOS8 adds new method to
UITableViewDelegate
:You can create customizable
UITableViewRowAction
which device your row actions.For example:
For more check here and here
Nice example of usage new API: here
正确的做法,只是为了让大家知道:
right way, just to let everyone know: