批量删除

关于删除在应用中很多也很常见,今天就是分享一下 细节性的问题,

 

问题:解决循环利用问题,删除时需要注意数组越界问题(更改模型数据,不能更改控件)

1.建立模型数据并有一个标识(来标识它是否选中)来标记数据模型

2.在自定义的cell中赋值

3,在控制器选中的状态中取反


代码:

1.模型

#import <Foundation/Foundation.h>


@interface JCModel : NSObject


/**标题 */

@property (nonatomic, copy) NSString *title;


/**price */

@property (nonatomic, copy) NSString *price;


/**buyCount */

@property (nonatomic, copy) NSString *buyCount;


/**icon */

@property (nonatomic, copy) NSString *icon;


@property (nonatomic, assign, getter=isChecked) BOOL checked;



+ (instancetype)jcModelWith:(NSDictionary *)dic;


- (instancetype)initModelWith:(NSDictionary *)dic;



@end



2.cell

#import <UIKit/UIKit.h>


#import "JCModel.h"


@interface JCCell : UITableViewCell



+(instancetype)cellWithTableView:(UITableView *)tableView;



/**

 <#name#>

 */

@property (nonatomic, strong) JCModel *model;


@end


@implementation JCCell



+ (instancetype)cellWithTableView:(UITableView *)tableView {

    static NSString *ID = @"Id";

    JCCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    

    if (cell == nil) {

        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];

    }

    

    return cell;

}



- (void)setModel:(JCModel *)model {

    _model = model;

    self.iconImageView.image = [UIImage imageNamed:model.icon];

    self.titleLabel.text = model.title;

    self.priceLabel.text = model.price;

    self.buyCountLabel.text = model.buyCount;

    

    self.checked.hidden = !model.checked;

}



- (void)awakeFromNib {

    

}



@end




#import "ViewController.h"

#import "JCModel.h"

#import "JCCell.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>




/**

 数据源

 */

@property (nonatomic, strong) NSMutableArray *dataSource;


///tableView

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end


@implementation ViewController




- (NSMutableArray *)dataSource {

    if (!_dataSource) {

        

        NSString *path = [[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil];

        

        NSArray *array = [NSArray arrayWithContentsOfFile:path];

        NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:array.count];

        for (NSDictionary *tempDic in array) {

            JCModel *model = [JCModel jcModelWith:tempDic];

            [newArray addObject:model];

        }

        _dataSource = newArray;

    }

    

    return _dataSource;

}



- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"%zd",self.dataSource.count);

    

}





- (IBAction)removeBtnAction:(UIButton *)sender {

    

    NSMutableArray *newArray = [NSMutableArray array];

    for (JCModel *model in self.dataSource) {

        

        if (model.isChecked) {

            

            [newArray addObject:model];

        }

    }

    

    NSLog(@"%@",newArray);

    

    [self.dataSource removeObjectsInArray:newArray];

    

    [self.tableView reloadData];

    

  

    

}




#pragma mark---tableView代理方法  UITableViewDataSource,UITableViewDelegate


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

    

    

    return self.dataSource.count;

}

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

    

    JCCell *cell = [JCCell cellWithTableView:tableView];

    cell.model = self.dataSource[indexPath.row];

  

    return cell;

    

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    return 80;

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    JCModel *model = self.dataSource[indexPath.row];

    model.checked = !model.isChecked;

    [tableView reloadData];

    

    

}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值