1、在IOS8之前,我們完成tableview中滑動顯示刪除,置頂,更多等等的按鈕時,都須要本身去完成,在IOS8中體系曾經寫好了,只需一個署理辦法和一個類就好了
2、IOS8的協定對了一個辦法,前往值是數組的tableview:editActionForRowAtIndexPath:辦法,我們可以在辦法外部寫好幾個按鈕,然後放到數組中前往,那些按鈕的類就是UITableviewRowAction
3、在UITableviewRowAction類。我們可以設置按鈕的款式,顯示文字、配景色和按鈕事宜(在block內完成)
4、在署理辦法中,我們可以罕見多個按鈕放到數組中前往,最早放入數組的按鈕顯示在最左邊,最初放入的顯示在最右邊
5、假如本身設定一個或多個按鈕,體系自帶的刪除按鈕就消逝了
設置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
辦法。
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0){
// 添加一個刪除按鈕
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
}];
}
else if (indexPath.row==1){
// 添加一個刪除按鈕
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
}];
// 添加一個修正按鈕
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修正" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了修正");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
}
else if (indexPath.row==2){
// 添加一個刪除按鈕
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
}];
// 添加一個修正按鈕
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修正" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了修正");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 添加一個發送按鈕
sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"發送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了發送");
}];
sanRowAction.backgroundColor=[UIColor orangeColor];
}
else{
// 添加一個刪除按鈕
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
}];
// 添加一個修正按鈕
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修正" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了修正");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 添加一個發送按鈕
sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"發送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了發送");
}];
sanRowAction.backgroundColor=[UIColor orangeColor];
// 添加一個發送按鈕
OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"OK鍵" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了OK");
}];
OK.backgroundColor=[UIColor purpleColor];
}
// 將設置好的按鈕放到數組中前往
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;
}
以上所述是小編給年夜家引見的iOS 中應用tableView完成右滑顯示選擇功效的相干材料,願望對年夜家有所贊助。
【iOS 中應用tableView完成右滑顯示選擇功效】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!