Ios 如何将图像添加到UITableViewCell删除按钮
我已经创建了一个可编辑的UITableView,现在我想在“删除”按钮中添加一个图像。到目前为止,我的代码是: 代码 请帮帮我 干杯Ios 如何将图像添加到UITableViewCell删除按钮,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我已经创建了一个可编辑的UITableView,现在我想在“删除”按钮中添加一个图像。到目前为止,我的代码是: 代码 请帮帮我 干杯 Palash我认为您不能在这个默认的删除按钮中插入任何内容,您需要构建自己的按钮。Ray Wenderlich的网站上有一个很好的指南,可以帮助您实现这一点: 我想这个答案也许是你想要的 引述: - (void)willTransitionToState:(UITableViewCellStateMask)state{ [super willTransitionT
Palash我认为您不能在这个默认的删除按钮中插入任何内容,您需要构建自己的按钮。Ray Wenderlich的网站上有一个很好的指南,可以帮助您实现这一点: 我想这个答案也许是你想要的 引述:
- (void)willTransitionToState:(UITableViewCellStateMask)state{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
for (UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[deleteBtn setImage:[UIImage imageNamed:@"delete.png"]];
[[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
[deleteBtn release];
}
}
}
}
迅速地
class TableViewRowAction: UITableViewRowAction
{
var image: UIImage?
func _setButton(button: UIButton) {
if let image = image, let titleLabel = button.titleLabel {
let labelString = NSString(string: titleLabel.text!)
let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: titleLabel.font])
button.tintColor = UIColor.whiteColor()
button.setImage(image.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
button.imageEdgeInsets.right = -titleSize.width
}
}
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let moreRowAction = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: " ") { action, indexPath in
// Action Block of more button.
}
moreRowAction.image = UIImage(named: "edit_white")
moreRowAction.backgroundColor = UIColor(red: 252/255, green: 65/255, blue: 75/255, alpha: 1.0)
let deleteRowAction = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: " ") { action, indexPath in
// Action Block of delete button.
}
deleteRowAction.backgroundColor = UIColor(red: 252/255, green: 65/255, blue: 75/255, alpha: 1.0)
deleteRowAction.image = UIImage(named: "delete_white")
return [deleteRowAction, moreRowAction]
}
您是否在项目中使用CoCoapod。查看“SWTableViewCell”。它可能会帮助您实现此功能
class TableViewRowAction: UITableViewRowAction
{
var image: UIImage?
func _setButton(button: UIButton) {
if let image = image, let titleLabel = button.titleLabel {
let labelString = NSString(string: titleLabel.text!)
let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: titleLabel.font])
button.tintColor = UIColor.whiteColor()
button.setImage(image.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
button.imageEdgeInsets.right = -titleSize.width
}
}
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let moreRowAction = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: " ") { action, indexPath in
// Action Block of more button.
}
moreRowAction.image = UIImage(named: "edit_white")
moreRowAction.backgroundColor = UIColor(red: 252/255, green: 65/255, blue: 75/255, alpha: 1.0)
let deleteRowAction = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: " ") { action, indexPath in
// Action Block of delete button.
}
deleteRowAction.backgroundColor = UIColor(red: 252/255, green: 65/255, blue: 75/255, alpha: 1.0)
deleteRowAction.image = UIImage(named: "delete_white")
return [deleteRowAction, moreRowAction]
}