表格多選刪除

一、系統方式

  • 將要刪除的數據添加到待刪數組中,從數據源中刪除待刪數組中包含的數據,刷新表格。
  • OC 中可設置編輯模式爲 UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; 或者設置 myTableView.allowsMultipleSelectionDuringEditing = YES; 進入多選模式。
  • 1.1 待刪數據數組初始化

// 聲明待刪數據數組
@property(nonatomic, retain)NSMutableArray *tempDeleteArray;

// 初始化待刪數據數組
tempDeleteArray = [[NSMutableArray alloc] init];
  • 1.2 自定義方法

// 編輯按鈕點擊響應事件
- (void)editClick:(UIButton *)button {

	// 改變編輯開關狀態
	[myTableView setEditing:!myTableView.editing animated:YES];

	// 設置編輯模式,容許編輯時多選,或者在協議方法中設置
	myTableView.allowsMultipleSelectionDuringEditing = YES;

	// 當編輯狀態發生改變的時候,清空待刪數組
	[tempDeleteArray removeAllObjects];

	[myTableView reloadData];
}

// 刪除按鈕點擊響應事件
- (void)deleteClick:(UIButton *)button {

	// 從數據源中刪除待選數組中包含的數據
	[myDataArray removeObjectsInArray:tempDeleteArray];

	// 清空待刪數組
	[tempDeleteArray removeAllObjects];

	[myTableView reloadData];
}
  • 1.3 UITableView 協議方法

// 設置編輯模式
/*
刪除、插入、多選刪除,不設置默認時爲刪除,
或者在編輯按鈕點擊事件中直接設置 myTableView.allowsMultipleSelectionDuringEditing = YES;
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

	// 多選刪除
	return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;                       
}

// 表格選中點擊響應事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	// 判斷 tableView 的編輯狀態,表格處於編輯狀態
	if (tableView.isEditing) {
		// 選中 cell 的時候,將對應的數據源模型添加到待刪除數組中
		[tempDeleteArray addObject:[myDataArray objectAtIndex:indexPath.row]];
	}
	else {
		// 恢復未選中狀態時的顏色
		[tableView deselectRowAtIndexPath:indexPath animated:YES];
	}
}

// 表格取消選中點擊響應事件
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

	// 判斷 tableView 的編輯狀態,表格處於編輯狀態
	if (tableView.isEditing) {
		// 將對應的數據模型從待刪除數組中移除
		[tempDeleteArray removeObject:[myDataArray objectAtIndex:indexPath.row]];
	}
}
  • 1.4 運行效果

  • ------

二、自定義方式 1

  • 2.1 XMGDeal.h

#import <Foundation/Foundation.h>

@interface XMGDeal : NSObject

@property (strong, nonatomic) NSString *buyCount;
@property (strong, nonatomic) NSString *price;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *icon;

/** 狀態量標識有無被打鉤 */
@property (assign, nonatomic, getter=isChecked) BOOL checked;

+ (instancetype)dealWithDict:(NSDictionary *)dict;

@end
  • 2.2 XMGDeal.m

#import "XMGDeal.h"

@implementation XMGDeal

+ (instancetype)dealWithDict:(NSDictionary *)dict {

	XMGDeal *deal = [[self alloc] init];
	[deal setValuesForKeysWithDictionary:dict];

	return deal;
}

@end
  • 2.3 XMGDealCell.xib

  • 2.4 XMGDealCell.h

#import <UIKit/UIKit.h>

@class XMGDeal;

@interface XMGDealCell : UITableViewCell

/** 團購模型數據 */
@property (nonatomic, strong) XMGDeal *deal;

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

@end
  • 2.5 XMGDealCell.m

#import "XMGDealCell.h"
#import "XMGDeal.h"

@interface XMGDealCell()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@property (weak, nonatomic) IBOutlet UIImageView *checkView;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

@end

@implementation XMGDealCell

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

	static NSString *ID = @"deal";
	XMGDealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
	if (cell == nil) {
		cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([XMGDealCell class]) 
		owner:nil 
		options:nil] lastObject];
	}
	return cell;
}

- (void)setDeal:(XMGDeal *)deal {
	_deal = deal;

	// 設置數據
	self.iconView.image = [UIImage imageNamed:deal.icon];
	self.titleLabel.text = deal.title;
	self.priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
	self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已購買", deal.buyCount];

	// 設置打鉤控件的顯示和隱藏
	self.checkView.hidden = !deal.isChecked;
}

@end
  • 2.6 XMGDealsViewController.m

#import "XMGDealsViewController.h"
#import "XMGDeal.h"
#import "XMGDealCell.h"

@interface XMGDealsViewController () <UITableViewDataSource, UITableViewDelegate>

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

/** 全部的團購數據 */
@property (nonatomic, strong) NSMutableArray *deals;

@end

@implementation XMGDealsViewController

- (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) {
			XMGDeal *deal = [XMGDeal dealWithDict:dict];
			[dealArray addObject:deal];
		}

		_deals = dealArray;
	}
	return _deals;
}

- (IBAction)remove {

	// 臨時數組:存放即將須要刪除的團購數據
	NSMutableArray *deletedDeals = [NSMutableArray array];
	for (XMGDeal *deal in self.deals) {
		if (deal.isChecked) [deletedDeals addObject:deal];
	}

	// 刪除模型數據
	[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 {

	XMGDealCell *cell = [XMGDealCell cellWithTableView:tableView];

	// 取出模型數據
	cell.deal = self.deals[indexPath.row];

	return cell;
}

#pragma mark - TableView 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	// 取消選中這一行
	[tableView deselectRowAtIndexPath:indexPath animated:YES];

	// 模型的打鉤屬性取反
	XMGDeal *deal = self.deals[indexPath.row];
	deal.checked = !deal.isChecked;

	// 刷新表格
	[tableView reloadData];
}

@end
  • 2.7 運行效果

  • ------

三、自定義方式 2

  • 3.1 XMGDeal.h

#import <Foundation/Foundation.h>

@interface XMGDeal : NSObject

@property (strong, nonatomic) NSString *buyCount;
@property (strong, nonatomic) NSString *price;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *icon;

+ (instancetype)dealWithDict:(NSDictionary *)dict;

@end
  • 3.2 XMGDeal.m

#import "XMGDeal.h"

@implementation XMGDeal

+ (instancetype)dealWithDict:(NSDictionary *)dict {

	XMGDeal *deal = [[self alloc] init];
	[deal setValuesForKeysWithDictionary:dict];

	return deal;
}

@end
  • 3.3 XMGDealCell.xib

  • 3.4 XMGDealCell.h

#import <UIKit/UIKit.h>

@class XMGDeal;

@interface XMGDealCell : UITableViewCell

/** 團購模型數據 */
@property (nonatomic, strong) XMGDeal *deal;

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

@end
  • 3.5 XMGDealCell.m

#import "XMGDealCell.h"
#import "XMGDeal.h"

@interface XMGDealCell()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@property (weak, nonatomic) IBOutlet UIImageView *checkView;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

@end

@implementation XMGDealCell

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

	static NSString *ID = @"deal";
	XMGDealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
	if (cell == nil) {
		cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([XMGDealCell class]) 
		owner:nil 
		options:nil] lastObject];
	}
	return cell;
}

- (void)setDeal:(XMGDeal *)deal {

	_deal = deal;

	// 設置數據
	self.iconView.image = [UIImage imageNamed:deal.icon];
	self.titleLabel.text = deal.title;
	self.priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
	self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已購買", deal.buyCount];
}

@end
  • 3.6 XMGDealsViewController.m

#import "XMGDealsViewController.h"
#import "XMGDeal.h"
#import "XMGDealCell.h"

@interface XMGDealsViewController () <UITableViewDataSource, UITableViewDelegate>

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

/** 全部的團購數據 */
@property (nonatomic, strong) NSMutableArray *deals;

/** 即將要刪除的團購 */
@property (nonatomic, strong) NSMutableArray *deletedDeals;

@end

@implementation XMGDealsViewController

- (NSMutableArray *)deletedDeals {

	if (!_deletedDeals) {
		_deletedDeals = [NSMutableArray array];
	}
	return _deletedDeals;
}

- (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) {
			XMGDeal *deal = [XMGDeal dealWithDict:dict];
			[dealArray addObject:deal];
		}
		_deals = dealArray;
	}
	return _deals;
}

- (IBAction)remove {

	// 刪除模型數據
	[self.deals removeObjectsInArray:self.deletedDeals];

	// 刷新表格
	[self.tableView reloadData];

	// 清空數組
	[self.deletedDeals removeAllObjects];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return self.deals.count;
}

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

	XMGDealCell *cell = [XMGDealCell cellWithTableView:tableView];

	// 取出模型數據
	cell.deal = self.deals[indexPath.row];

	cell.checkView.hidden = ![self.deletedDeals containsObject:cell.deal];

	return cell;
}

#pragma mark - TableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	// 取消選中這一行
	[tableView deselectRowAtIndexPath:indexPath animated:YES];

	// 取出模型
	XMGDeal *deal = self.deals[indexPath.row];
	if ([self.deletedDeals containsObject:deal]) {
		[self.deletedDeals removeObject:deal];
	} else {
		[self.deletedDeals addObject:deal];
	}

	// 刷新表格
	[tableView reloadData];
}
@end
  • 3.7 運行效果

  • ------
相關文章
相關標籤/搜索