受到丹尼回答的启发。这是他的代码的objective-c版本。
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Do you really want to delete this comment?" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alertView setTag:101];
[alertView show];
}];
UITableViewCell *commentCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
CGFloat height = commentCell.frame.size.height;
UIImage *backgroundImage = [self deleteImageForHeight:height];
deleteAction.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
return @[deleteAction];
}
下面是图像绘制方法:
- (UIImage*)deleteImageForHeight:(CGFloat)height{
CGRect frame = CGRectMake(0, 0, 62, height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(62, height), NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, frame);
UIImage *image = [UIImage imageNamed:@"icon-delete"];
[image drawInRect:CGRectMake(frame.size.width/2.0, frame.size.height/2.0 - 10, 18, 20)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
结果是: