typedef NS_ENUM(NSUInteger, EditTypes) {
// 删除状态
deleteForEditTypes = 0,
// 编辑状态
editForEditTypes,
};
@interface FavoriteBoard ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
// 编辑
@property (weak, nonatomic) IBOutlet UIBarButtonItem *btnEdit;
// 选择全部
@property (weak, nonatomic) IBOutlet UIButton *btnSelectAll;
// 删除
@property (weak, nonatomic) IBOutlet UIButton *btnDelete;
// 数据源
@property (strong, nonatomic) NSMutableArray *arrFavorites;
// 选择的cell
@property (strong, nonatomic) NSMutableArray *arrSelectRows;
@property (assign, nonatomic) EditTypes editType;
@property (strong, nonatomic) Favorite *favorite;
@end
@implementation FavoriteBoard
@synthesize arrFavorites;
@synthesize arrSelectRows;
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = nil;
self.editType = deleteForEditTypes;
self.arrSelectRows = [NSMutableArray array];
self.arrFavorites = [NSMutableArray array];
[self.toolBar setHidden: YES];
[self.btnDelete setSelected: NO];
[self.btnDelete setEnabled: NO];
[self getFavoriteList];
}
/**
* @brief 获取收藏列表
*/
- (void)getFavoriteList
{
[INKFavouriteViewModel favoriteListWithCallback:^(id data, INKResponseError *error) {
self.arrFavorites = data;
if (self.arrFavorites.count != 0) {
self.noFavouriteView.hidden = YES;
}
[self.tableView reloadData];
}];
}
- (void)viewWillAppear:(BOOL)animated
{
if (self.arrFavorites.count == 0) {
[self.btnEdit setEnabled:NO];
self.noFavouriteView.hidden = NO;
}
}
- (void)viewDidDisappear:(BOOL)animated {
[self.toolBar setHidden:YES];
}
#pragma mark - 编辑状态
- (IBAction)btnEdit:(id)sender {
if ([self.btnEdit.title isEqualToString:@"编辑"]) {
self.btnEdit.title = @"取消";
self.editType = editForEditTypes;
[self.tableView setEditing:YES animated:YES];
[self.toolBar setHidden:NO];
}
else
{
self.editType = deleteForEditTypes;
self.btnEdit.title = @"编辑";
[self.tableView setEditing:NO animated:YES];
[self.toolBar setHidden:YES];
self.btnDelete.selected = NO;
self.btnDelete.enabled = NO;
[arrSelectRows removeAllObjects];
}
}
#pragma mark - 返回
- (IBAction)btnBack:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 选择全部
- (IBAction)btnSelectAllFavourite:(id)sender {
if (self.arrSelectRows.count != self.arrFavorites.count) {
[self.arrSelectRows removeAllObjects];
[self.arrSelectRows addObjectsFromArray:self.arrFavorites];
for (int i = 0; i < self.arrSelectRows.count; i ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
// 遍历数组,将所有元素置为选中状态
[self.tableView selectRowAtIndexPath:indexPath
animated:YES
scrollPosition:UITableViewScrollPositionNone];
}
[self changeButtonStatus];
}
else {
for (int i = 0; i < self.arrSelectRows.count; i ++) {
// 取消选中状态
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
[self.arrSelectRows removeAllObjects];
[self changeButtonStatus];
}
}
- (void)deleteToSelectIndexPath
{
[INKFavouriteAndLikeViewModel favoriteCancels:(NSArray *)self.arrSelectRows
withCallback:^(id data, INKResponseError *error) {
if (error == nil) {
[arrFavorites removeObjectsInArray:arrSelectRows];
[arrSelectRows removeAllObjects];
[self changeButtonStatus];
if (self.arrFavorites.count == 0) {
self.noFavouriteView.hidden = NO;
[self.view reloadInputViews];
}
[self.tableView reloadData];
}
}];
}
#pragma mark - 多选删除
- (IBAction)btnDeleteFavourite:(id)sender {
[INKFavouriteAndLikeViewModel favoriteCancels:(NSArray *)self.arrSelectRows
withCallback:^(id data, INKResponseError *error) {
if (error == nil) {
[arrFavorites removeObjectsInArray:arrSelectRows];
[arrSelectRows removeAllObjects];
[self changeButtonStatus];
if (self.arrFavorites.count == 0) {
self.noFavouriteView.hidden = NO;
[self.view reloadInputViews];
}
[self.tableView reloadData];
}
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrFavorites.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *favoriteImageCell = @"favoriteImageCell";
NSString *favoriteLocationCell = @"favoriteLocationCell";
NSString *favoriteTextCell = @"favoriteTextCell";
NSString *currentCell = nil;
Favorite *favorite = [self.arrFavorites objectAtIndex:indexPath.row];
if (favorite.location != nil) {
currentCell = favoriteLocationCell;
}
else {
if (favorite.imageName != nil) {
currentCell = favoriteImageCell;
}
else {
currentCell = favoriteTextCell;
}
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:currentCell forIndexPath:indexPath];
[self configCell:cell indexPath:indexPath];
return cell;
}
- (void)configCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath
{
self.favorite = [self.arrFavorites objectAtIndex:indexPath.row];
cell.data = self.favorite;
}
#pragma mark - 编辑后的选中与取消选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.favorite = [self.arrFavorites objectAtIndex:indexPath.row];
if (self.editType == editForEditTypes) {
[self.arrSelectRows addObject:self.favorite];
[self changeButtonStatus];
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.favorite = [self.arrFavorites objectAtIndex:indexPath.row];
if (self.editType == editForEditTypes) {
[self.arrSelectRows removeObject:self.favorite];
INFO(@"de arrSelectRows: %lu", (unsigned long)arrSelectRows.count);
INFO(@"de arrFavorites : %lu", (unsigned long)arrFavorites.count);
[self changeButtonStatus];
}
}
#pragma mark - 允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
#pragma mark - 改变编辑状态
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editType == editForEditTypes) {
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
#pragma mark - 单选删除,修改方式
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[INKFavouriteAndLikeViewModel favoriteCancel:self.favorite
withCallback:^(id data, INKResponseError *error) {
if (error == nil) {
[self.arrFavorites removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
if (self.arrFavorites.count == 0) {
self.noFavouriteView.hidden = NO;
}
}
}];
}
else if (editingStyle == (UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete)) {
}
}
#pragma mark - 修改的状态
- (void)changeButtonStatus
{
if (arrSelectRows.count > 0) {
self.btnDelete.selected = YES;
self.btnDelete.enabled = YES;
}
else {
self.btnDelete.selected = NO;
self.btnDelete.enabled = NO;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end