// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
#import "XMGWine.h"
#import "XMGWineCell.h"
#import "MJExtension.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
/** 酒模型數(shù)組*/
@property (nonatomic, strong) NSMutableArray *wines;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
#pragma mark - 懶加載數(shù)據(jù)
- (NSMutableArray *)wines
{
if (_wines == nil) {
_wines = [XMGWine mj_objectArrayWithFilename:@"wine.plist"];
}
return _wines;
}
NSString *ID = @"wine";
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = 100;
// 注冊方法
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGWineCell class]) bundle:nil] forCellReuseIdentifier:ID];
}
/**
* 刪除模型
*/
- (IBAction)remove{
// 可變數(shù)組
NSMutableArray *deleteArray = [NSMutableArray array];
// 保存要刪除的模型數(shù)據(jù)
for (XMGWine *wine in self.wines) {
if (wine.isChecked) {
[deleteArray addObject:wine];
}
}
// 刪除數(shù)據(jù)
[self.wines removeObjectsInArray:deleteArray];
// 刷新
[self.tableView reloadData];
}
#pragma mark -UITableViewDataSource方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.wines.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 去緩存取
XMGWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 傳遞模型數(shù)據(jù)
cell.wine = self.wines[indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 獲取數(shù)據(jù)
XMGWine *wine = self.wines[indexPath.row];
// 取反
wine.checked = !wine.isChecked;
// 刷新
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
@end
// 酒模型數(shù)據(jù)
// XMGWine.h
#import <Foundation/Foundation.h>
@interface XMGWine : NSObject
/** 圖片*/
@property (nonatomic, strong) NSString *image;
/** 價錢*/
@property (nonatomic, strong) NSString *money;
/** 酒名*/
@property (nonatomic, strong) NSString *name;
/** 是否選中*/
/** checked,默認是NO*/
@property (nonatomic, assign, getter=isChecked) BOOL checked;
@end
// XMGWine.m
#import "XMGWine.h"
@implementation XMGWine
@end
// XMGWineCell.h
#import <UIKit/UIKit.h>
@class XMGWine;
@interface XMGWineCell : UITableViewCell
/** 酒模型*/
@property (nonatomic, strong) XMGWine *wine;
@end
// XMGWineCell.m
#import "XMGWineCell.h"
#import "XMGWine.h"
@interface XMGWineCell ()
/** 圖片*/
@property (nonatomic, weak) IBOutlet UIImageView *iconImageView;
/** 名稱*/
@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
/** 價格*/
@property (nonatomic, weak) IBOutlet UILabel *moneyLabel;
/** 勾選圖片*/
@property (nonatomic, weak) IBOutlet UIImageView *checkedImageView;
@end
@implementation XMGWineCell
- (void)setWine:(XMGWine *)wine
{
_wine = wine;
self.iconImageView.image = [UIImage imageNamed:wine.image];
self.nameLabel.text = wine.name;
self.moneyLabel.text = [NSString stringWithFormat:@"¥%@", wine.money];
// 是否隱藏
self.checkedImageView.hidden = !wine.isChecked;
}
@end
XMGWineCell.xib圖片:

批量刪除前效果圖片:

批量刪除后效果圖片:

- 筆者認為最重要的思想是:cell會重用,要想不被cell重用影響而產(chǎn)生一些錯誤,要深刻領悟MVC架構思想,View要顯示的數(shù)據(jù)是由Model來提供的,操作界面的變幻,最好是通過修改模型來間接影響界面的變幻,這才是王道