知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
ios8tableView設(shè)置滑動(dòng)刪除時(shí)顯示多個(gè)按鈕
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):46
鑌哥,研究良久最后才發(fā)明iOS8 TableView出新功能,然后就記下來(lái),供大年夜家參考,為大年夜伙所用。
看我博客都知道,我一貫都是愛好代碼注釋結(jié)合,供給demo給大年夜伙參考,寫得不好,不要見怪哦。
**
* tableView:editActionsForRowAtIndexPath: // 設(shè)置滑動(dòng)刪除時(shí)顯示多個(gè)按鈕
* UITableViewRowAction // 經(jīng)由過(guò)程詞攀類創(chuàng)建按鈕
* 1. 我們?cè)趹?yīng)用一些應(yīng)用的時(shí)刻,在滑動(dòng)一些接洽人的某一行的時(shí)刻,會(huì)出現(xiàn)刪除、置頂、更多等等的按鈕,在iOS8之前,我們都須要本身去實(shí)現(xiàn)。然則,到了iOS8,體系已經(jīng)寫好了,只須要一個(gè)代勞辦法和一個(gè)類就搞定了
* 2. iOS8的協(xié)定多了一個(gè)辦法,返回值是數(shù)組的tableView:editActionsForRowAtIndexPath:辦法,我們可以在辦法內(nèi)部寫好幾個(gè)按鈕,然后放到數(shù)組中返回,那些按鈕的類就是UITableViewRowAction
* 3. 在UITableViewRowAction類,我們可以設(shè)置按鈕的樣式、顯示的文字、配風(fēng)景、和按鈕的事宜(事宜在Block中實(shí)現(xiàn))
* 4. 在代勞辦法中,我們可以創(chuàng)建多個(gè)按鈕放到數(shù)組中返回,最先放入數(shù)組的按鈕顯示在最右側(cè),最后放入的顯示在最左側(cè)
* 5. 留意:如不雅我們本身設(shè)定了一個(gè)或多個(gè)按鈕,體系自帶的刪除按鈕就消掉了...
下面就寫下demo讓大年夜家參考:
//
// ViewController.m
// UITableView-Demo
//
// Created by huweibin on 15/1/4.
// Copyright (c) 2014年 . All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *allDataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addAllViews];
[self loadAllData];
}
#pragma mark 添加全部頁(yè)面元素
- (void)addAllViews
{
self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
#warning iOS8 - 瓜分線樣式
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
_tableView.separatorEffect = blurEffect;
// 注冊(cè)
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellIdentifier"];
// 右上角編輯按鈕
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark 加載所稀有據(jù)
- (void)loadAllData
{
self.allDataArray =[[NSMutableArray alloc] initWithObjects:@"王菲",@"周迅",@"李冰冰",@"白冰",@"紫薇",@"馬蘇",@"劉詩(shī)詩(shī)",@"趙薇",@"angelbaby",@"熊黛林",nil];
}
#pragma mark 編輯按鈕
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[_tableView setEditing:!_tableView.isEditing animated:YES];
}
#pragma mark - UITableViewDataSource Methods
#pragma mark 設(shè)置有若干分組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark 設(shè)置每個(gè)分組有若干行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _allDataArray.count;
}
#pragma mark 設(shè)置某行上顯示的內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.textLabel.text = _allDataArray[indexPath.row];
// cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0f];
return cell;
}
#pragma mark 設(shè)置可以進(jìn)行編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
#pragma mark 設(shè)置編輯的樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
#pragma mark 設(shè)置處理編輯情況
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 1. 更新數(shù)據(jù)
[_allDataArray removeObjectAtIndex:indexPath.row];
// 2. 更新UI
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
#pragma mark 設(shè)置可以移動(dòng)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
#pragma mark 處理移動(dòng)的情況
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 1. 更新數(shù)據(jù)
NSString *title = _allDataArray[sourceIndexPath.row];
[_allDataArray removeObject:title];
[_allDataArray insertObject:title atIndex:destinationIndexPath.row];
// 2. 更新UI
[tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
/**
* tableView:editActionsForRowAtIndexPath: // 設(shè)置滑動(dòng)刪除時(shí)顯示多個(gè)按鈕
* UITableViewRowAction // 經(jīng)由過(guò)程詞攀類創(chuàng)建按鈕
* 1. 我們?cè)趹?yīng)用一些應(yīng)用的時(shí)刻,在滑動(dòng)一些接洽人的某一行的時(shí)刻,會(huì)出現(xiàn)刪除、置頂、更多等等的按鈕,在iOS8之前,我們都須要本身去實(shí)現(xiàn)。But,到了iOS8,體系已經(jīng)寫好了,只須要一個(gè)代勞辦法和一個(gè)類就搞定了
* 2. iOS8的<UITableViewDelegate>協(xié)定多了一個(gè)辦法,返回值是數(shù)組的tableView:editActionsForRowAtIndexPath:辦法,我們可以在辦法內(nèi)部寫好幾個(gè)按鈕,然后放到數(shù)組中返回,那些按鈕的類就是UITableViewRowAction
* 3. 在UITableViewRowAction類,我們可以設(shè)置按鈕的樣式、顯示的文字、配風(fēng)景、和按鈕的事宜(事宜在Block中實(shí)現(xiàn))
* 4. 在代勞辦法中,我們可以創(chuàng)建多個(gè)按鈕放到數(shù)組中返回,最先放入數(shù)組的按鈕顯示在最右側(cè),最后放入的顯示在最左側(cè)
* 5. 留意:如不雅我們本身設(shè)定了一個(gè)或多個(gè)按鈕,體系自帶的刪除按鈕就消掉了...
*/
#warning iOS8 -
#pragma mark 在滑著手勢(shì)刪除某一行的時(shí)刻,顯示出更多的按鈕
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加一個(gè)刪除按鈕
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點(diǎn)擊了刪除");
// 1. 更新數(shù)據(jù)
[_allDataArray removeObjectAtIndex:indexPath.row];
// 2. 更新UI
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
// // 刪除一個(gè)置頂按鈕
// UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// NSLog(@"點(diǎn)擊了置頂");
//
// // 1. 更新數(shù)據(jù)
// [_allDataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
//
// // 2. 更新UI
// NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
// [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
// }];
// topRowAction.backgroundColor = [UIColor blueColor];
// 添加一個(gè)更多按鈕
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點(diǎn)擊了更多");
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 將設(shè)置好的按鈕放到數(shù)組中返回
// return @[deleteRowAction, topRowAction, moreRowAction];
return @[deleteRowAction,moreRowAction];
}
相關(guān)案例查看更多
相關(guān)閱讀
- 排名
- 汽車報(bào)廢回收管理系統(tǒng)
- 網(wǎng)站建設(shè)選
- web開發(fā)技術(shù)
- 網(wǎng)站建設(shè)專業(yè)品牌
- 制作一個(gè)小程序
- 小程序分銷商城
- 服務(wù)器
- 微信分銷系統(tǒng)
- 用戶登錄
- 小程序
- 昆明網(wǎng)站開發(fā)
- 專業(yè)網(wǎng)站建設(shè)公司
- 昆明小程序開發(fā)聯(lián)系方式
- 網(wǎng)站制作哪家好
- 云南網(wǎng)站制作
- 小程序生成海報(bào)
- 網(wǎng)站建設(shè)哪家強(qiáng)
- 網(wǎng)站建設(shè)
- 汽車報(bào)廢管理
- 網(wǎng)站建設(shè)報(bào)價(jià)
- 云南微信小程序開發(fā)
- 云南旅游網(wǎng)站建設(shè)
- 小程序用戶登錄
- 云南網(wǎng)站建設(shè)專業(yè)品牌
- 云南做軟件
- 政府網(wǎng)站建設(shè)服務(wù)
- 報(bào)廢車回收
- 網(wǎng)站開發(fā)
- 昆明做網(wǎng)站建設(shè)的公司排名