1.效果图
2.关键代码
#import "DealsViewController.h"
#import "Deal.h"
#import "DealCell.h"
@interface DealsViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** 所有的团购数据 */
@property (nonatomic, strong) NSMutableArray *deals;
@end
@implementation DealsViewController
- (NSMutableArray *)deals
{
if (_deals == nil) {
// 加载plist中的字典数组
NSString *path = [[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 字典数组 -> 模型数组
NSMutableArray *dealArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
Deal *deal = [Deal dealWithDict:dict];
[dealArray addObject:deal];
}
_deals = dealArray;
}
return _deals;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 允许在编辑模式进行多选操作
self.tableView.allowsMultipleSelectionDuringEditing = YES;
}
- (IBAction)multiOperation:(id)sender {
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
- (IBAction)remove {
// 获得所有被选中的行
NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
// 便利所有的行号
NSMutableArray *deletedDeals = [NSMutableArray array];
for (NSIndexPath *path in indexPaths) {
[deletedDeals addObject:self.deals[path.row]];
}
// 删除模型数据
[self.deals removeObjectsInArray:deletedDeals];
// 刷新表格 一定要刷新数据
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.deals.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 创建cell
DealCell *cell = [DealCell cellWithTableView:tableView];
// 取出模型数据
cell.deal = self.deals[indexPath.row];
return cell;
}
#pragma mark - TableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end