UITableView自定义系统左滑Cell菜单
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"标记为\n已回访";
}
// 设置显示多个按钮
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction * deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"屏蔽用户" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
}];
deleteRowAction.backgroundColor = [UIColor redColor];
UITableViewRowAction * topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"取消关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
}];
topRowAction.backgroundColor = [UIColor blueColor];
return @[deleteRowAction,topRowAction];
}
在cell中自定义样式
- (void)layoutSubviews
{
[super layoutSubviews];
// 去掉隐式动画
[CATransaction begin];
[CATransaction setDisableActions:YES];
[self setupSlideBtn];
[CATransaction commit];
}
// 设置左滑菜单按钮的样式
- (void)setupSlideBtn
{
for (UIView *subView in self.subviews) {
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
// 屏蔽用户
UIView *smsContentView = subView.subviews[0];
for (UIView *smsView in smsContentView.subviews) {
[smsView removeAllSubviews];
}
UIImageView *smsImage = [[UIImageView alloc] init];
smsImage.image = [UIImage imageNamed:@"UserView_follow2"];
smsImage.frame = smsContentView.bounds;
[smsContentView addSubview:smsImage];
// 取消关注
UIView *messageContentView = subView.subviews[1];
for (UIView *messageView in messageContentView.subviews) {
[messageView removeAllSubviews];
}
UIImageView *messageImage = [[UIImageView alloc] init];
messageImage.image = [UIImage imageNamed:@"UserView_follow1"];
messageImage.frame = messageContentView.bounds;
[messageContentView addSubview:messageImage];
}
}
}
使用panCell自定义左滑菜单
1、创建 cell 继承 PanTableViewCell
2、添加PanTableViewCellDelegate协议方法
3、实现协议方法
#import "PanCellButton.h"
- (NSArray<PanCellButton *> *)panTableViewCell:(PanTableViewCell *)cell tableView:(UITableView *)tableView rightPanCellButtonsAtIndexPath:(NSIndexPath *)indexPath{
PanCellButton *removeFollowUserButton = [[PanCellButton alloc] initWithTitle:nil image:[UIImage imageNamed:@"UserView_follow1"] onClickCallback:^(UIButton *panButton) {
[self CancelFollowAction];
}];
PanCellButton *hiddenUserButton = [[PanCellButton alloc] initWithTitle:nil image:[UIImage imageNamed:@"UserView_follow2"] onClickCallback:^(UIButton *panButton) {
[self ShieldingUser];
}];
return @[hiddenUserButton,removeFollowUserButton];
}
效果图
下载使用
https://github.com/githubze/panCell