国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS應用中UITableView左滑自定義選項及批量刪除的實現

2019-10-21 18:55:42
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了iOS應用中UITableView左滑自定義選項及批量刪除的實現,UITableView列表中即通訊錄左滑呼出選項的那種效果在刪除時能夠實現多行刪除將更加方便,需要的朋友可以參考下
 

實現UITableView左滑自定義選項
當UITableView進入編輯模式,在進行左滑操作的cell的右邊,默認會出現Delete按鈕,如何自定義左滑出現的按鈕呢?

只需要實現UITableView下面的這個代理方法。

復制代碼代碼如下:

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *likeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"喜歡" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
      // 實現相關的邏輯代碼
      // ...
      // 在最后希望cell可以自動回到默認狀態,所以需要退出編輯模式
      tableView.editing = NO;
    }];

 

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
      // 首先改變model
      [self.books removeObjectAtIndex:indexPath.row];
      // 接著刷新view
      [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
      // 不需要主動退出編輯模式,上面更新view的操作完成后就會自動退出編輯模式
    }];

    return @[deleteAction, likeAction];
}


此時左滑就會出現兩個按鈕,一個是喜歡,另一個是刪除。出現的順序和在這個方法中返回的數組中的元素順序相關。

 

如果實現了上述方法,那么之前提到過的tableView:commitEditingStyle:forRowAtIndexPath:和tableView: titleForDeleteConfirmationButtonForRowAtIndexPath:方法就不會再調用了。(如果為了兼容以前的版本,那么需要實現tableView:commitEditingStyle:forRowAtIndexPath:方法,在這個方法里什么都不用做即可。)


UITableview的多行同時刪除
下面這段代碼配合xib使用, 不過關鍵不在這地方,記住后面的使用到的委托。

其實質就是數組array的刪除操作。

復制代碼代碼如下:

//
//  UITableViewDelteMutilRowsViewController.m
//  UITableViewDelteMutilRows
//

 

#import "UITableViewDelteMutilRowsViewController.h"

@implementation UITableViewDelteMutilRowsViewController
@synthesize tableview;
@synthesize dataArray;
@synthesize deleteDic;
@synthesize leftButton;
@synthesize rightButton;
#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
    dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
    deleteDic = [[NSMutableDictionary alloc] init];

    rightButton.title = @"編輯";
}

- (IBAction)choseData{
    if (rightButton.title == @"編輯") {
        rightButton.title = @"確定";
        [self.tableview setEditing:YES animated:YES];
    }
    else {
        rightButton.title = @"編輯";
        [deleteDic removeAllObjects];
        [self.tableview setEditing:NO animated:YES];
    }

}
- (IBAction)deleteFuntion{
    [dataArray removeObjectsInArray:[deleteDic allKeys]];
    
    [self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithArray:[deleteDic allValues]] withRowAnimation:UITableViewRowAnimationFade];
    [deleteDic removeAllObjects];
    
}

- (void)dealloc {
    [leftButton release];
    [rightButton release];
    [deleteDic release];
    [dataArray release];
    [tableview release];
    [super dealloc];
}
#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [dataArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell...
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    return cell;
}


/*//這里設置為可滑動編輯刪除
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (rightButton.title== @"確定") {
        [deleteDic setObject:indexPath forKey:[dataArray objectAtIndex:indexPath.row]];   
    }
    else {
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (rightButton.title == @"確定") {
        [deleteDic removeObjectForKey:[dataArray objectAtIndex:indexPath.row]];
    }
}
@end



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会东县| 墨玉县| 清河县| 木兰县| 九龙坡区| 广宗县| 东宁县| 涿州市| 平昌县| 姚安县| 太谷县| 牙克石市| 茌平县| 濮阳市| 兴文县| 丘北县| 璧山县| 诏安县| 迁西县| 甘洛县| 秭归县| 紫金县| 克东县| 浏阳市| 会昌县| 拜城县| 樟树市| 夏津县| 西安市| 河曲县| 静安区| 社旗县| 崇左市| 青海省| 射阳县| 滦平县| 祁阳县| 邮箱| 轮台县| 天门市| 双流县|