Iphone UITableViewCell中的滑动删除有白色背景,需要清除
我正在尝试更改当您滑动UITableViewCell行时显示的视图的背景色,即“删除”按钮后面的背景色 我试过换手机。编辑AccessoryView,但没用Iphone UITableViewCell中的滑动删除有白色背景,需要清除,iphone,ios,uitableview,uicolor,Iphone,Ios,Uitableview,Uicolor,我正在尝试更改当您滑动UITableViewCell行时显示的视图的背景色,即“删除”按钮后面的背景色 我试过换手机。编辑AccessoryView,但没用 UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero]; myBackgroundView.backgroundColor = [UIColor clearColor]; cell.editingAccessoryView = myBa
UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
myBackgroundView.backgroundColor = [UIColor clearColor];
cell.editingAccessoryView = myBackgroundView;
有什么想法吗?你就快到了。
UITableViewCell
类有一个backgroundView
属性,默认为nil
。只需像您在问题中所做的那样创建一个新的UIView
,然后将其分配给单元格的backgroundView
属性
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.backgroundView.backgroundColor = [UIColor clearColor];
我认为这取决于你如何将内容添加到你的手机中
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.backgroundView.backgroundColor = [UIColor clearColor];
当我直接使用[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属性了。我已经试过了,效果非常好 我之所以回答这个问题,是因为我花了一段时间才找到答案,这是搜索中出现的第一个条目。通过使用
willDisplayCell
方法,您可以访问单元格的背景色。请注意,[UIColor clearColor]代码>将返回白色,因此相应地调整代码
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor blackColor];
}
虽然这些答案是正确的,但我觉得在大多数情况下,在interface builder中设置单元格的背景颜色更容易。我指的是单元格的实际背景色属性,而不是它的内容视图。如果内容视图的颜色始终是内容视图的颜色,则没有理由动态执行此操作。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的好例子:正确的方法,让每个人都知道:
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
}
}
}
你有没有找到解决办法?我也有同样的问题:)