向左滑动cell出现删除和分享按钮,要求显示图片没有文字

最近做项目有个需求是向左滑动cell出现删除和分享按钮,要求显示图片没有文字。代码:实现一下tableView.m (- (nullable NSArray)tableView:(UITableView )tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED; )

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

//UITableViewRowAction是iOS8才有的,title不想要打了空格占着大小

UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"          " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

NSLog(@"点击删除");

}];

UITableViewRowAction *shareRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"          " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

NSLog(@"点击分享");

}];

// 这个地方:先加入的在右边

return @[shareRowAction, deleteRowAction];

}

cell.m (在- (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:delete];

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:share];

shareImage.frame = CGRectMake(0, 0, shareView.frame.size.width, shareView.frame.size.height);

[shareView addSubview:shareImage];

}

}

}

效果图

WechatIMG53.jpeg

看图层发现加了很多图片的,因为layoutSubviews会调用多次,可以把控件懒加载就行了,或者其实UITableViewCellDeleteConfirmationView里的控件本质上是UIButton,直接设置图片。

你可能感兴趣的:(向左滑动cell出现删除和分享按钮,要求显示图片没有文字)