UITableView上的批量操作(系统方法)

//
//  TableViewController.m


#import "TableViewController.h"
#import "tgModel.h"
#import "tgCell.h"

@interface TableViewController ()
/** 所有团购数据 */
@property(nonatomic,strong) NSMutableArray *tgs;


- (IBAction)clickedDeleteButton:(UIBarButtonItem *)sender;
- (IBAction)clickedEditButton:(UIBarButtonItem *)sender;

@end

@implementation TableViewController

- (NSMutableArray *)tgs
{
    if (_tgs == nil) {
        // 加载plist文件中的字典数组
        NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 字典数组 -> 模型数组
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            tgModel *model = [tgModel tgWithDict:dict];
            [arrayM addObject:model];
        }
        
        _tgs = arrayM;
    }
    return _tgs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //允许在编辑模式进行多选操作
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    self.navigationItem.rightBarButtonItem.title = nil;
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tgs.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建cell的过程封装到cell类中
    tgCell *cell = [tgCell cellWithTableView:tableView];
    
    //传递模型数据
    cell.model = self.tgs[indexPath.row];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (IBAction)clickedDeleteButton:(UIBarButtonItem *)sender
{
    // 获取所有被选中的行
    NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
    
    //遍历所有的行号
    NSMutableArray *deleteds = [NSMutableArray array];
    for (NSIndexPath *path in indexPaths) {
        [deleteds addObject:self.tgs[path.row]];
    }
    
    //删除模型数据
    [self.tgs removeObjectsInArray:deleteds];
    
    //刷新表格
//    [self.tableView reloadData];
    //用下面这个方法有动画效果
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:YES];
}

/**
 *  点击了导航栏左边按钮
 */
- (IBAction)clickedEditButton:(UIBarButtonItem *)sender
{
    //切换编辑模式
    if (self.tableView.isEditing) {
        [self.tableView setEditing:NO animated:YES];
        //更改leftBarButtonItem上的文字
        self.navigationItem.leftBarButtonItem.title = @"编辑";
        self.navigationItem.rightBarButtonItem.title = nil;
    }else{
        [self.tableView setEditing:YES animated:YES];
        //更改leftBarButtonItem上的文字
        self.navigationItem.leftBarButtonItem.title = @"取消";
        self.navigationItem.rightBarButtonItem.title = @"删除";
    }
}
@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值