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

載自 http://demo.netfoucs.com/sammyieveo/article/details/42418115數組

 

iOS8 TableView出新功能,而後就記下來,供你們參考,爲大夥所用。dom

看我博客都知道,我一貫都是喜歡代碼註釋結合,提供demo給大夥參考,寫得很差,不要見怪哦。atom

 

**spa

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

 *  UITableViewRowAction                        //經過此類建立按鈕代理

 *  1. 咱們在使用一些應用的時候,在滑動一些聯繫人的某一行的時候,會出現刪除、置頂、更多等等的按鈕,在iOS8以前,咱們都須要本身去實現。可是,到了iOS8,系統已經寫好了,只須要一個代理方法和一個類就搞定了orm

 *  2. iOS8的協議多了一個方法,返回值是數組的tableView:editActionsForRowAtIndexPath:方法,咱們能夠在方法內部寫好幾個按鈕,而後放到數組中返回,那些按鈕的類就是UITableViewRowAction事件

 *  3. 在UITableViewRowAction類,咱們能夠設置按鈕的樣式、顯示的文字、背景色、和按鈕的事件(事件在Block中實現)ci

 *  4. 在代理方法中,咱們能夠建立多個按鈕放到數組中返回,最早放入數組的按鈕顯示在最右側,最後放入的顯示在最左側rem

 *  5. 注意:若是咱們本身設定了一個或多個按鈕,系統自帶的刪除按鈕就消失了...

 

 

 

下面就寫下demo讓你們參考:

 

 

//

//  ViewController.m

//  UITableView-Demo

//

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

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

//

 

#import "ViewController.h"

 

@interfaceViewController () <UITableViewDataSource,UITableViewDelegate>

 

@property (nonatomic,retainUITableView *tableView;

@property (nonatomic,retainNSMutableArray *allDataArray;

 

@end

@implementation ViewController

 

- (void)viewDidLoad {

    [superviewDidLoad];

    [selfaddAllViews];

    [selfloadAllData];

    

}

 

#pragma mark 添加所有頁面元素

- (void)addAllViews

{

    self.tableView = [[UITableViewallocinitWithFrame:[UIScreenmainScreen].boundsstyle:UITableViewStylePlain];

    _tableView.delegate =self;

    _tableView.dataSource =self;

    [self.viewaddSubview:_tableView];

    

#warning iOS8 - 分割線樣式

    UIBlurEffect *blurEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];

    UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffecteffectForBlurEffect:blurEffect];

    _tableView.separatorEffect = blurEffect;

    

    //註冊

    [_tableViewregisterClass:[UITableViewCellclassforCellReuseIdentifier:@"cellIdentifier"];

    

    //右上角編輯按鈕

    self.navigationItem.rightBarButtonItem =self.editButtonItem;

}

 

#pragma mark 加載全部數據

- (void)loadAllData

{

    self.allDataArray =[[NSMutableArrayalloc]initWithObjects:@"王菲",@"周迅",@"李冰冰",@"白冰",@"紫薇",@"馬蘇",@"劉詩詩",@"趙薇",@"angelbaby",@"熊黛林",nil];

}

 

 

#pragma mark 編輯按鈕

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

{

    [supersetEditing:editing animated:animated];

    

    [_tableViewsetEditing:!_tableView.isEditinganimated:YES];

}

 

 

 

#pragma mark - UITableViewDataSource Methods

#pragma mark 設置有多少分組

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return1;

}

 

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

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

{

    return_allDataArray.count;

}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"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

{

    returnYES;

}

#pragma mark 設置編輯的樣式

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

{

    returnUITableViewCellEditingStyleDelete;

}

#pragma mark 設置處理編輯狀況

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

{

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        // 1.更新數據

        [_allDataArrayremoveObjectAtIndex:indexPath.row];

        

        // 2.更新UI

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

    }

    

}

 

#pragma mark 設置能夠移動

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

{

    returnYES;

}

#pragma mark 處理移動的狀況

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

{

    // 1. 更新數據

    NSString *title =_allDataArray[sourceIndexPath.row];

    [_allDataArrayremoveObject:title];

    [_allDataArrayinsertObject:title atIndex:destinationIndexPath.row];

    

    // 2. 更新UI

    [tableView moveRowAtIndexPath:sourceIndexPathtoIndexPath:destinationIndexPath];

}

 

 

/**

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

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

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

 *  2. iOS8<UITableViewDelegate>協議多了一個方法,返回值是數組的tableView:editActionsForRowAtIndexPath:方法,咱們能夠在方法內部寫好幾個按鈕,而後放到數組中返回,那些按鈕的類就是UITableViewRowAction

 *  3. UITableViewRowAction類,咱們能夠設置按鈕的樣式、顯示的文字、背景色、和按鈕的事件(事件在Block中實現)

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

 *  5. 注意:若是咱們本身設定了一個或多個按鈕,系統自帶的刪除按鈕就消失了...

 */

 

 

#warning iOS8 -

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

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

{

    // 添加一個刪除按鈕

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

        NSLog(@"點擊了刪除");

        

        // 1. 更新數據

        [_allDataArrayremoveObjectAtIndex: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:UITableViewRowActionStyleNormaltitle:@"更多"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {

        NSLog(@"點擊了更多");

        

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

    }];

    moreRowAction.backgroundEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];

    

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

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

    return@[deleteRowAction,moreRowAction];

}

相關文章
相關標籤/搜索