如何将图像添加到UITableViewCell删除按钮

时间:2015-02-15 10:54:22

标签: ios objective-c uitableview

我制作了一个可编辑的UITableView,现在我想在删除按钮中添加一个Image。直到现在我的代码是这样的:

代码

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"×" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:indexPath];

        if (record) {
            [self.fetchedResultsController.managedObjectContext deleteObject:record];
        }
}];
button.backgroundColor = UIColorFromRGB(0x0d9de5); //arbitrary color

return @[button];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

}

请帮助我!

干杯,

Palash

4 个答案:

答案 0 :(得分:2)

我不认为您可以在此默认删除按钮中插入任何内容,并且您需要构建自己的删除按钮。 Ray Wenderlich的网站上有一个很好的指南可以帮助你实现这个目标:check this link ......

答案 1 :(得分:2)

在Swift中

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]

}

答案 2 :(得分:1)

您是否在项目中使用CocoaPods。查看“SWTableViewCell”。它可以帮助您实现此功能。

https://cocoapods.org/pods/SWTableViewCell

答案 3 :(得分:0)

我认为这个答案可能就是您正在寻找的https://stackoverflow.com/a/12511432/3342901

引用:

- (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];
        }
    }
}
}