关于删除在应用中很多也很常见,今天就是分享一下 细节性的问题,
问题:解决循环利用问题,删除时需要注意数组越界问题(更改模型数据,不能更改控件)
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];
}