iOS UITableViewCell滑動刪除


首先,我們初始化一個界面,以列表的形式展示

1
2
3
4
5
6
7
8
9
10
11
#pragma mark - 初始化UI
- ( void )initUI{
     self .view.backgroundColor = RGB(242, 242, 247);
     self .automaticallyAdjustsScrollViewInsets =  NO ;
     sideslipTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, kScreenWidth, kScreenHeight - 60) style:UITableViewStylePlain];
     sideslipTableView.backgroundColor = [UIColor clearColor];
     sideslipTableView.delegate =  self ;
     sideslipTableView.dataSource =  self ;
     sideslipTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
     [ self .view addSubview:sideslipTableView];
}

然后,准備數據源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
     UITableView *sideslipTableView;
     //可變數組,用於刪除數據
     NSMutableArray *dataArray;
     
}
 
@end
 
@implementation ViewController
 
- ( void )viewDidLoad {
     [ super viewDidLoad];
     
     [ self initUI];
     dataArray = [ NSMutableArray arrayWithArray: @[ @"1111" , @"2222" , @"3333" , @"4444" , @"5555" , @"6666" , @"7777" , @"8888" , @"9999" ]];
}

接下來我們要將數據顯示出來

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma mark - 行
- ( NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:( NSInteger )section{
     return dataArray.count;
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath{
     return 46;
}
#pragma mark - cell內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
     static NSString *indefier =  @"cell" ;
     sideslipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indefier];
     if (!cell) {
         cell = [[sideslipTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indefier];
     }
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.lable.text = dataArray[indexPath.row];
     return cell;
}

最后,實現UITableView的一些代理方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//先要設Cell可編輯
- ( BOOL )tableView:(UITableView *)tableView canEditRowAtIndexPath:( NSIndexPath *)indexPath
{
     return YES ;
}
//定義編輯樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:( NSIndexPath *)indexPath
{
     return UITableViewCellEditingStyleDelete;
}
//進入編輯模式,按下出現的編輯按鈕后,進行刪除操作
- ( void )tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:( NSIndexPath *)indexPath
{
     if (editingStyle == UITableViewCellEditingStyleDelete) {
         [dataArray removeObjectAtIndex:indexPath.row];
         // Delete the row from the data source.
         [sideslipTableView deleteRowsAtIndexPaths:[ NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
     }
}
//修改編輯按鈕文字
- ( NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:( NSIndexPath *)indexPath
{
     return @"刪除" ;
}

這樣就可以實現UITableViewCell滑動刪除的效果啦。
效果圖:
效果圖1效果圖2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM