1.首先創(chuàng)建tableTest,并初始化可變數(shù)組arr.
#pragma mark 懶加載初始化table
- (UITableView *)tableTest{
if (!_tableTest) {
_tableTest = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
_tableTest.delegate = self;
_tableTest.dataSource =self;
}
return _tableTest;
}
#pragma mark 系統(tǒng)方法-didload
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.tableTest.tableFooterView = [[UIView alloc]init];
self.arr = [NSMutableArray arrayWithCapacity:0];
NSArray *arrtemp = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"];
[self.arr addObjectsFromArray:arrtemp];
[self.view addSubview:self.tableTest];
}
2.完成table的代理方法
3.刪除操作
#pragma mark 刪除行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
// 先刪除模型
[self.arr removeObjectAtIndex:indexPath.row];
// 再刷新數(shù)據
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
4.根據需要 修改默認的delete
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪除";
}
5.當有多個操作按鈕時候祝闻,類似于QQ上的向左滑動操作
- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"+關注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"關注了");
//收回左側滑動按鈕 并退出編輯
tableView.editing = NO;
}];
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self.arr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
NSLog(@"刪除了");
}];
UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"未讀" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
tableView.editing = NO;
NSLog(@"未讀");
}];
// 改變滑動的背景色 (根據需要修改顏色)
action0.backgroundColor = [UIColor blueColor];
action1.backgroundColor = [UIColor orangeColor];
action2.backgroundColor = [UIColor greenColor];
return @[action0,action1,action2];
}
效果圖如下: