啥都不多说了,直接上代码。加上去就可以跑了
-
(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *(^imageWithView)(UIView *) = ^(UIView *view) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
};UIColor *(^getColorWithLabelText)(NSString *, UIColor *, UIColor *) = ^(NSString *text, UIColor *textColor, UIColor *bgColor) {
UILabel *actionLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
actionLb.font = [UIFont boldSystemFontOfSize:14];
actionLb.text = text;
actionLb.textColor = textColor;
actionLb.textAlignment = NSTextAlignmentCenter;
actionLb.backgroundColor = bgColor;return [UIColor colorWithPatternImage:imageWithView(actionLb)];
};
// 添加按钮
UITableViewRowAction *rowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" "handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了");
}];
rowAction1.backgroundColor = getColorWithLabelText(@"row1", [UIColor whiteColor], [UIColor lightGrayColor]);
UITableViewRowAction *rowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" "handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了");
}];
transferRowAction.backgroundColor = getColorWithLabelText(@"row2", [UIColor blackColor], [UIColor yellowColor]);
return @[rowAction1, rowAction2];
}