如何使用UITableViewRowAction實(shí)現(xiàn)右滑選擇呢暑塑?
1、在iOS8以前驾茴,我們實(shí)現(xiàn)tableview中滑動(dòng)顯示刪除盼樟,置頂,更多等等的按鈕時(shí)锈至,都需要自己去實(shí)現(xiàn)晨缴,在iOS8中系統(tǒng)已經(jīng)寫好了,只要一個(gè)代理方法和一個(gè)類就行了
2峡捡、iOS8的協(xié)議對(duì)了一個(gè)方法击碗,返回值是數(shù)組的tableview:editActionForRowAtIndexPath:方法,我們可以在方法內(nèi)部寫好幾個(gè)按鈕们拙,然后放到數(shù)組中返回稍途,那些按鈕的類就是UITableviewRowAction
3、在UITableviewRowAction類砚婆。我們可以設(shè)置按鈕的樣式械拍,顯示文字、背景色和按鈕事件(在block內(nèi)實(shí)現(xiàn))
4装盯、在代理方法中坷虑,我們可以常見多個(gè)按鈕放到數(shù)組中返回,最先放入數(shù)組的按鈕顯示在最右邊验夯,最后放入的顯示在最左邊
5猖吴、如果自己設(shè)定一個(gè)或多個(gè)按鈕,系統(tǒng)自帶的刪除按鈕就消失了
- 設(shè)置tableView可以編輯
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return
YES;
}
- UITableViewRowAction的使用方法:
+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;
- 重寫UITableViewDelegate的
- (nullable NSArray<uitableviewrowaction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath</uitableviewrowaction *>
方法挥转。
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
if
(indexPath.row==0)
{
// 添加一個(gè)刪除按鈕
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@
"刪除"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@
"點(diǎn)擊了刪除"
);
}];
}
else
if
(indexPath.row==1)
{
// 添加一個(gè)刪除按鈕
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@
"刪除"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@
"點(diǎn)擊了刪除"
);
}];
// 添加一個(gè)修改按鈕
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@
"修改"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@
"點(diǎn)擊了修改"
);
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
}
else
if
(indexPath.row==2)
{
// 添加一個(gè)刪除按鈕
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@
"刪除"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@
"點(diǎn)擊了刪除"
);
}];
// 添加一個(gè)修改按鈕
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@
"修改"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@
"點(diǎn)擊了修改"
);
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 添加一個(gè)發(fā)送按鈕
sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@
"發(fā)送"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@
"點(diǎn)擊了發(fā)送"
);
}];
sanRowAction.backgroundColor=[UIColor orangeColor];
}
else
{
// 添加一個(gè)刪除按鈕
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@
"刪除"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@
"點(diǎn)擊了刪除"
);
}];
// 添加一個(gè)修改按鈕
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@
"修改"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@
"點(diǎn)擊了修改"
);
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 添加一個(gè)發(fā)送按鈕
sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@
"發(fā)送"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@
"點(diǎn)擊了發(fā)送"
);
}];
sanRowAction.backgroundColor=[UIColor orangeColor];
// 添加一個(gè)發(fā)送按鈕
OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@
"OK鍵"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@
"點(diǎn)擊了OK"
);
}];
OK.backgroundColor=[UIColor purpleColor];
}
// 將設(shè)置好的按鈕放到數(shù)組中返回
if
(indexPath.row==0)
{
return
@[deleteRowAction];
}
else
if
(indexPath.row==1)
{
return
@[deleteRowAction,moreRowAction];
}
else
if
(indexPath.row==2)
{
return
@[deleteRowAction,moreRowAction,sanRowAction];
}
else
if
(indexPath.row==3)
{
return
@[deleteRowAction,moreRowAction,sanRowAction,OK];
}
return
nil;
}