UITableviewCell花式操作之删除置顶和标记
效果展示
滑动后侧边栏出现删除、置顶和标记功能
下面直接上代码。。。
对于创建全局数组和创建UITableview这个我就直接跳过了(数组最好是用可变数组)
- (void)viewDidLoad {
[super viewDidLoad];
cellArray = [NSMutableArray array];
for (int i = 0; i < 20; i ++) {
[cellArray addObject:[NSString stringWithFormat:@"%d",i]];
}
screeenWidth = [UIScreen mainScreen].bounds.size.width;
screenHeight = [UIScreen mainScreen].bounds.size.height;
[self makeTableView];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)makeTableView{
_myTableView = [[UITableView alloc]initWithFrame:self.view.frame];
_myTableView.delegate = self;
_myTableView.dataSource = self;
[self.view addSubview:_myTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return cellArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *indetifier = @"celllllllll";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
}
cell.textLabel.text = cellArray[indexPath.row];
return cell;
}
直接上UITableview的代理
//这个是直接添加侧滑栏选项的代理,只支持在8.0之后使用
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED{
// 删除选项
UITableViewRowAction *deleAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[cellArray removeObject:[cellArray objectAtIndex:indexPath.row]];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}];
deleAction.backgroundColor = [UIColor redColor];
// 置顶选项
UITableViewRowAction *firstR = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self tableView:tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}];
firstR.backgroundColor = [UIColor lightGrayColor];
// 标记选项,活着说是特殊操作选项。。。一般是针对数据进行处理
UITableViewRowAction *markerCell = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"标记未读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSString *biaoshiStr = [cellArray objectAtIndex:indexPath.row];
[cellArray removeObject:biaoshiStr];
biaoshiStr = [NSString stringWithFormat:@"%@+%@",biaoshiStr,biaoshiStr];
[cellArray insertObject:biaoshiStr atIndex:indexPath.row];
[tableView reloadData];
}];
markerCell.backgroundColor = [UIColor orangeColor];
return @[firstR,markerCell,deleAction];
}
然后就添加具体的视线方法,部分方法可以直接在UITableViewRowAction带的block方法块里面视线,但是部分方法需要其它代理的配合
比如说置顶方法
//置顶实现代码
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSString *fromStr = cellArray[sourceIndexPath.row];
[cellArray removeObject:fromStr];
[cellArray insertObject:fromStr atIndex:destinationIndexPath.row];
[tableView reloadData];
}
下面配置一些必要的代理
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
我这是不是就直接把demo代码贴上去了,在贴几个效果图
删除效果展示
标记效果展示
置顶效果展示
至于标记这个选项,原理就是对数据进行操作,具体怎么操作,根据自己的需求自己考虑,方法就在那里。。。。阿达!!!