一、侧滑按钮的添加:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath){
NSLog(@"删除");
}];
UITableViewRowAction *toTopRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath){
NSLog(@"置顶");
}];
UITableViewRowAction *otherRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"其他" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath){
NSLog(@"其他");
}];
toTopRowAction.backgroundColor = [UIColor orangeColor];
otherRowAction.backgroundColor = [UIColor lightGrayColor];
return @[deleteRowAction, toTopRowAction, otherRowAction];
}
二、侧滑按钮的自定义:
//在cell类内的layoutSubviews实现如下方法
- (void)layoutSubviews {
for (UIView *subView in self.subviews) {
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
// 拿到subView之后再获取子控件
// 因为上面删除按钮是第二个加的所以下标是1
UIView *deleteConfirmationView = subView.subviews[1]
//改背景颜色
deleteConfirmationView.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:42.0/255.0 blue:62.0/255.0 alpha:1.0]
for (UIView *deleteView in deleteConfirmationView.subviews) {
NSLog(@"%@",deleteConfirmationView.subviews)
UIImageView *deleteImage = [[UIImageView alloc] init]
deleteImage.contentMode = UIViewContentModeScaleAspectFit
deleteImage.image = [UIImage imageNamed:@"share"]
deleteImage.frame = CGRectMake(0, 0, deleteView.frame.size.width, deleteView.frame.size.height)
[deleteView addSubview:deleteImage]
}
// 这里是右边的
UIView *shareConfirmationView = subView.subviews[0]
shareConfirmationView.backgroundColor = [UIColor colorWithRed:142.0/255.0 green:201.0/255.0 blue:75.0/255.0 alpha:1.0]
for (UIView *shareView in shareConfirmationView.subviews) {
UIImageView *shareImage = [[UIImageView alloc] init]
shareImage.contentMode = UIViewContentModeScaleAspectFit
shareImage.image = [UIImage imageNamed:@"w"]
shareImage.frame = CGRectMake(0, 0, shareView.frame.size.width, shareView.frame.size.height)
[shareView addSubview:shareImage]
}
}
}
}