iOS中給UITableView的側滑刪除增加多個按鈕的實現(xiàn)方法
一. 需求:
cell的側滑刪除默認只有一個刪除按鈕, 給側滑添加多個按鈕, '刪除', '置頂', '更多'.
二. 實現(xiàn)說明:
1) 我們在使用一些應用的時候,在滑動一些聯(lián)系人的某一行的時候,會出現(xiàn)刪除、置頂、更多等等的按鈕,在iOS8之前,我們都需要自己去實現(xiàn)。但是到了iOS8,系統(tǒng)已經(jīng)寫好了,只需要一個代理方法和一個類就搞定了
2) iOS8的協(xié)議多了一個方法,返回值是數(shù)組的tableView:editActionsForRowAtIndexPath:方法,我們可以在方法內部寫好幾個按鈕,然后放到數(shù)組中返回,那些按鈕的類就是UITableViewRowAction
3) 在UITableViewRowAction類,我們可以設置按鈕的樣式、顯示的文字、背景色、和按鈕的事件(事件在Block中實現(xiàn))
4) 在代理方法中,我們可以創(chuàng)建多個按鈕放到數(shù)組中返回,最先放入數(shù)組的按鈕顯示在最右側,最后放入的顯示在最左側
5) 注意:如果我們自己設定了一個或多個按鈕,系統(tǒng)自帶的刪除按鈕就消失了.
三. 代碼實現(xiàn):
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加一個'刪除'按鈕
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了'刪除'");
// 1. 更新數(shù)據(jù)
// 2. 更新UI
// [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}];
// 添加一個'置頂'按鈕
UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了'置頂'");
// 1. 更新數(shù)據(jù)
// 2. 更新UI
//NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
//[tableView moveRowAtIndexPath:indexPathtoIndexPath:firstIndexPath];
}];
topRowAction.backgroundColor = [UIColor blueColor];
// 添加一個'更多'按鈕
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了'更多'");
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
//將設置好的按鈕放到數(shù)組中返回
return @[deleteRowAction, topRowAction, moreRowAction];
}
相關閱讀:
詳解iOS開發(fā)中UITableview cell 頂部空白的多種設置方法
以上所述是小編給大家介紹的Android中給UITableView的側滑刪除增加多個按鈕的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Android開發(fā)中MJRefresh自定義刷新動畫效果
本文給大家介紹了MJRefresh自定義刷新動畫效果,包括常見用法等相關知識,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧2016-11-11
Android下拉刷新SwipeRefreshLayout控件使用方法
這篇文章主要介紹了Android下拉刷新SwipeRefreshLayout控件使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11

