ios8 tableView設定滑動刪除時顯示多個按鈕

來源:互聯網
上載者:User

ios8 tableView設定滑動刪除時顯示多個按鈕

* tableView:editActionsForRowAtIndexPath: // 設定滑動刪除時顯示多個按鈕

* UITableViewRowAction // 通過此類建立按鈕

* 1. 我們在使用一些應用的時候,在滑動一些連絡人的某一行的時候,會出現刪除、置頂、更多等等的按鈕,在iOS8之前,我們都需要自己去實現。但是,到了iOS8,系統已經寫好了,只需要一個代理方法和一個類就搞定了

* 2. iOS8的協議多了一個方法,傳回值是數組的tableView:editActionsForRowAtIndexPath:方法,我們可以在方法內部寫好幾個按鈕,然後放到數組中返回,那些按鈕的類就是UITableViewRowAction

* 3. 在UITableViewRowAction類,我們可以設定按鈕的樣式、顯示的文字、背景色、和按鈕的事件(事件在Block中實現)

* 4. 在代理方法中,我們可以建立多個按鈕放到數組中返回,最先放入數組的按鈕顯示在最右側,最後放入的顯示在最左側

* 5. 注意:如果我們自己設定了一個或多個按鈕,系統內建的刪除按鈕就消失了...


 

 

下面就寫下demo讓大家參考:

 

 

//

// ViewController.m

// UITableView-Demo

//

// Created by huweibin on 15/1/4.

// Copyright (c) 2014年 . All rights reserved.

//

 

#import ViewController.h

 

@interface ViewController ()

 

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *allDataArray;

 

@end

@implementation ViewController

 

- (void)viewDidLoad {

[super viewDidLoad];

[self addAllViews];

[self loadAllData];

 

}

 

#pragma mark 添加全部頁面元素

- (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;

 

// 註冊

[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@cellIdentifier];

 

// 右上方編輯按鈕

self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

 

#pragma mark 載入所有資料

- (void)loadAllData

{

self.allDataArray =[[NSMutableArray alloc] initWithObjects:@王菲,@周迅,@李冰冰,@白冰,@紫薇,@馬蘇,@劉詩詩,@趙薇,@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 設定有多少分組

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

 

#pragma mark 設定每個分組有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return _allDataArray.count;

}

#pragma mark 設定某行上顯示的內容

- (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 設定可以進行編輯

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

#pragma mark 設定編輯的樣式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath

{

return UITableViewCellEditingStyleDelete;

}

#pragma mark 設定處理編輯情況

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete) {

// 1. 更新資料

[_allDataArray removeObjectAtIndex:indexPath.row];

 

// 2. 更新UI

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

 

}

 

#pragma mark 設定可以移動

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

#pragma mark 處理移動的情況

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

// 1. 更新資料

NSString *title = _allDataArray[sourceIndexPath.row];

[_allDataArray removeObject:title];

[_allDataArray insertObject:title atIndex:destinationIndexPath.row];

 

// 2. 更新UI

[tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

}

 

 

/**

* tableView:editActionsForRowAtIndexPath: // 設定滑動刪除時顯示多個按鈕

* UITableViewRowAction // 通過此類建立按鈕

* 1. 我們在使用一些應用的時候,在滑動一些連絡人的某一行的時候,會出現刪除、置頂、更多等等的按鈕,在iOS8之前,我們都需要自己去實現。But,到了iOS8,系統已經寫好了,只需要一個代理方法和一個類就搞定了

* 2. iOS8的協議多了一個方法,傳回值是數組的tableView:editActionsForRowAtIndexPath:方法,我們可以在方法內部寫好幾個按鈕,然後放到數組中返回,那些按鈕的類就是UITableViewRowAction

* 3. 在UITableViewRowAction類,我們可以設定按鈕的樣式、顯示的文字、背景色、和按鈕的事件(事件在Block中實現)

* 4. 在代理方法中,我們可以建立多個按鈕放到數組中返回,最先放入數組的按鈕顯示在最右側,最後放入的顯示在最左側

* 5. 注意:如果我們自己設定了一個或多個按鈕,系統內建的刪除按鈕就消失了...

*/

 

 

#warning iOS8 -

#pragma mark 在滑動手勢刪除某一行的時候,顯示出更多的按鈕

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

// 添加一個刪除按鈕

UITableViewRowAction *deleteRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDestructive title:@刪除 handler:^(UITableViewRowAction*action, NSIndexPath *indexPath) {

NSLog(@點擊了刪除);

 

// 1. 更新資料

[_allDataArray removeObjectAtIndex:indexPath.row];

// 2. 更新UI

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}];

 

// // 刪除一個置頂按鈕

// UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@置頂 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

// NSLog(@點擊了置頂);

//

// // 1. 更新資料

// [_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];

 

// 添加一個更多按鈕

UITableViewRowAction *moreRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormal title:@更多 handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {

NSLog(@點擊了更多);

 

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

}];

moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

 

// 將設定好的按鈕放到數組中返回

// return @[deleteRowAction, topRowAction, moreRowAction];

return @[deleteRowAction,moreRowAction];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.