UITableViewCell中滑动删除有白色背景,需要清除

发布于 2024-12-23 11:30:51 字数 337 浏览 1 评论 0原文

我试图更改滑动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一指流沙 2024-12-30 11:30:51

我回答这个问题是因为我花了一段时间才找到答案,这是搜索中出现的第一个条目。通过使用 willDisplayCell 方法,您可以访问单元格背景颜色。请注意,[UIColor clearColor]; 将返回白色,因此请相应地调整您的代码。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell.backgroundColor = [UIColor blackColor];

}

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.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell.backgroundColor = [UIColor blackColor];

}
国粹 2024-12-30 11:30:51

你快到了。 UITableViewCell 类有一个 backgroundView 属性,默认情况下为 nil。只需按照您在问题中所做的那样创建一个新的 UIView ,然后将其分配给单元格的 backgroundView 属性即可。

cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.backgroundView.backgroundColor = [UIColor clearColor];

You're almost there. The UITableViewCell class has a backgroundView property, which is nil by default. Just create a new UIView as you've done in your question, then assign that to the backgroundView property of your cell.

cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.backgroundView.backgroundColor = [UIColor clearColor];
三岁铭 2024-12-30 11:30:51

我认为这取决于您如何将内容添加到单元格中。

当我使用 [cell addSubView] 或 [cell.contentView addSubView] 直接将内容添加到单元格时,我遇到了同样的问题。

我的解决方法是:

Create a view
Add all your content(labels, images etc;) to this view
Finally then add the view to your cell using [cell addSubView:tmpView]

并且您不再需要篡改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:

Create a view
Add all your content(labels, images etc;) to this view
Finally then add the view to your cell using [cell addSubView:tmpView]

And you do not need to tamper with the backgroundView property anymore. I have tried this and works perfectly!

—━☆沉默づ 2024-12-30 11:30:51

虽然这些答案是正确的,但我认为在大多数情况下,在界面生成器中设置单元格的背景颜色会更容易。我的意思是单元格的实际背景颜色属性,而不是它的内容视图。如果它始终是内容视图的颜色,则没有理由动态地执行此操作。

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.

dawn曙光 2024-12-30 11:30:51

iOS8 为 UITableViewDelegate 添加了新方法:

optional func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?

您可以创建可自定义的 UITableViewRowAction 来控制行操作。

例如:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction = UITableViewRowAction(style: .destructive, title: "Cancel", handler: { action, indexPath in 
         // DELETE ROW HERE
     })
    deleteAction.background = .green // you can change background color

    return [deleteAction]
}

有关更多信息,请查看此处此处

使用新 API 的好示例:此处

iOS8 adds new method to UITableViewDelegate:

optional func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?

You can create customizable UITableViewRowAction which device your row actions.

For example:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction = UITableViewRowAction(style: .destructive, title: "Cancel", handler: { action, indexPath in 
         // DELETE ROW HERE
     })
    deleteAction.background = .green // you can change background color

    return [deleteAction]
}

For more check here and here

Nice example of usage new API: here

把时间冻结 2024-12-30 11:30:51

正确的做法,只是为了让大家知道:

func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
    for subview in tableView.subviews {
        if NSStringFromClass(type(of: subview)) == "UISwipeActionPullView", let button = subview.subviews.first, NSStringFromClass(type(of: button)) == "UISwipeActionStandardButton"
        {
            button.backgroundColor = .clear
            button.subviews.first?.backgroundColor = .clear
        }
    }
}

right way, just to let everyone know:

func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
    for subview in tableView.subviews {
        if NSStringFromClass(type(of: subview)) == "UISwipeActionPullView", let button = subview.subviews.first, NSStringFromClass(type(of: button)) == "UISwipeActionStandardButton"
        {
            button.backgroundColor = .clear
            button.subviews.first?.backgroundColor = .clear
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文