大多初學(xué)者對tableview實現(xiàn)刪除蜜唾,點擊cell是都能實現(xiàn)的柏靶,但對于多選宣肚,單選卻不是那么理解导坟。屿良。
寫了一個demo可供參考(不當之處請及時指出)
1,在.h文件中聲明一個變量type,用于區(qū)分進入當前頁面是多選惫周,單選還是刪除
#import <UIKit/UIKit.h>
@interface TableView_chooseViewController : UIViewController
@property (nonatomic,copy)NSString *type;//multiple 多選 single 單選 delete 刪除
@end
2尘惧,在.m中聲明一個用于儲存你選中內(nèi)容的數(shù)組,在demo中我用的是selectedArray 递递,重點在于聲明UITableViewCellEditingStyle (全局化)editingStyle喷橙;
#import "TableView_chooseViewController.h"
#import "TableViewCell.h"
@interface TableView_chooseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *listArray;
@property (nonatomic,strong) NSMutableArray *selectedArray;//選中項所用數(shù)組
@property (nonatomic,assign) UITableViewCellEditingStyle editingStyle;//用于區(qū)分tableview的多選 單選 及滑動刪除操作
@end
3,然后在viewDidLoad中根據(jù)type類型提前對editingStyle賦值
- (void)viewDidLoad {
[super viewDidLoad];
[self.listArray addObjectsFromArray:@[@"張三",@"李四",@"王二",@"麻子"]];
self.tableview.delegate = self;
self.tableview.dataSource = self;
self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
if ([self.type isEqualToString:@"multiple"]) {
self.title = @"多選";
self.editingStyle = UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
self.tableview.allowsMultipleSelection = YES;
//定義右上角按鈕
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"確定" style:UIBarButtonItemStylePlain target:self action:@selector(okButton)];
self.navigationItem.rightBarButtonItem = button;
}else if ([self.type isEqualToString:@"single"]) {
self.title = @"單選";
self.editingStyle = UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
self.tableview.allowsSelection = YES;
//定義右上角按鈕
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"確定" style:UIBarButtonItemStylePlain target:self action:@selector(okButton)];
self.navigationItem.rightBarButtonItem = button;
}else {
self.title = @"滑動刪除";
self.editingStyle = UITableViewCellEditingStyleDelete ;
}
// Do any additional setup after loading the view.
}
4登舞,這個比較容易理解就是簡單的tableViewCell的調(diào)用
#pragma mark --UITableView delegate datasoure
//返回section的個數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//返回cell的個數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.listArray.count;
}
//加載TableViewCell,布局cell中的內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil] firstObject];
}
cell.type = self.type;
cell.nameLabel.text = self.listArray[indexPath.row];
return cell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == self.listArray.count) {
return UITableViewCellEditingStyleInsert;
}else{
return self.editingStyle;
}
}
5贰逾,根據(jù)type類型 用didSelectRowAtIndexPath和didDeselectRowAtIndexPath來實現(xiàn)多選及單選
//tableView點擊事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *string = [self.listArray objectAtIndex:indexPath.row];
if ([self.type isEqualToString:@"multiple"]) {
[self.selectedArray addObject:string];
}else if ([self.type isEqualToString:@"single"]) {
[self.selectedArray addObject:string];
}else{
}
}
//取消tableView選中 點擊事件
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *string = [self.listArray objectAtIndex:indexPath.row];
NSArray *array = [self.selectedArray mutableCopy];
if ([self.type isEqualToString:@"multiple"]) {
for (NSString *str in array) {
if ([string isEqualToString:str]) {
[self.selectedArray removeObject:string];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}else if ([self.type isEqualToString:@"single"]){
[self.selectedArray removeObject:string];
}else{
}
}
6,這個就是大家常見的滑動刪除了逊躁,不多說看代碼
//刪除按鈕的title賦值
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪除";
}
//刪除用到的函數(shù)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(editingStyle == UITableViewCellEditingStyleDelete){
if (self.listArray.count > 0) {
[self.listArray removeObjectAtIndex:indexPath.row];
[_tableview reloadData];
}
}
}
7似踱,寫到這里這個demo基本就是完成了隅熙,但考慮到有很多伸手黨稽煤,我還是決定把TableViewCell.m的內(nèi)容貼出來
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
self.selectImage.image = [UIImage imageNamed:@"charter_tick"];
}else{
self.selectImage.image = [UIImage imageNamed:@"charter_select"];
}
// Configure the view for the selected state
}
-(void)layoutSubviews{
[super layoutSubviews];
if ([self.type isEqualToString:@"multiple"]||[self.type isEqualToString:@"single"]) {
self.selectImage.hidden = NO;
self.leftCont.constant = 50;
}else{
self.selectImage.hidden = YES;
self.leftCont.constant = 10;
}
}