开始做单选和多选的时候在网上找了好久,发现要么是单选,要么是多选,共存的教程几乎没有。因此自己研究了下,写了个简单的demo用于分享
注意:看完后你可以得到什么?
1.单选删除的实现
2.多选删除的实现。
3.单选和多选的共存。
4.多选按钮的定制。
5.左滑添加按钮
实现
语法:Object-C
1.简单的创建应用等这里不再赘述,将直接在ViewController中进行操作
2.写了四个属性
@property (nonatomic, strong) NSMutableArray *dataArray;//数据源
@property (nonatomic, strong) UITableView *listTableView;
@property (nonatomic, strong) NSMutableArray *selectedArray;//存储被选择的数据
@property (nonatomic, strong) NSMutableArray *deleteIndexPaths;//存储indexpath
重写tableView的get方法。
- (UITableView *)listTableView {
if (!_listTableView) {
_listTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_listTableView.delegate = self;
_listTableView.dataSource = self;
//必须要加上,实现多选的必要方法
_listTableView.allowsMultipleSelectionDuringEditing = YES;
}
return _listTableView;
}
viewDidLoad的实现
//初始化数组
_selectedArray = [NSMutableArray array];
_deleteIndexPaths = [NSMutableArray array];
_dataArray = [NSMutableArray array];
//导航栏按钮用系统定制,当然如果你自定义导航栏,按钮需要自己定制
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//初始化数据源
for (int i = 0; i < 10; i++) {
[self.dataArray addObject:[NSString stringWithFormat:@"row %d",i]];
}
//创建tableview和button
[self createUI];
-(void)createUI {
[self.view addSubview:self.listTableView];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setTitle:@"删除" forState:UIControlStateNormal];
[deleteButton setBackgroundColor:[UIColor orangeColor]];
deleteButton.frame = CGRectMake(0, CGRectGetMaxY(self.view.frame) - 40, self.view.frame.size.width, 40);
[self.view addSubview:deleteButton];
[deleteButton addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
//点击删除的实现方法
- (void)deleteButtonClick:(UIButton *)button
{
[_dataArray removeObjectsInArray:_selectedArray];
[_listTableView deleteRowsAtIndexPaths:_deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[_deleteIndexPaths removeAllObjects];
[_selectedArray removeAllObjects];
NSLog(@"buttonClick:");
}
//更新编辑和done之间的切换及tableview的编辑状态。
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
if ([_listTableView isEditing]) {
self.editButtonItem.title = @"Edit";
[_listTableView setEditing:NO animated:YES];
} else {
self.editButtonItem.title = @"Done";
[_listTableView setEditing:YES animated:YES];
}
}
tableviewDataSource的实现
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[ UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
tableview Delegate的实现
单选删除的实现 当然如果下面左滑打算添加按钮,这里可不实现,在下面的方法中实现
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[_dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
多选的选择实现,删除方法在上面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([tableView isEditing]) {
//说明 在这里对左边选择的圆圈按钮进行定制,系统默认的为蓝色,这里有时候可能层级会发生过变化,因此做了判断。
if (cell.subviews.count > 3) {
if (cell.subviews[3].subviews[0]) {
((UIImageView *)(cell.subviews[3].subviews[0])).image = [UIImage imageNamed:@"xz"];
}
} else {
if (cell.subviews[2].subviews[0]) {
((UIImageView *)(cell.subviews[2].subviews[0])).image = [UIImage imageNamed:@"xz"];
}
}
[_selectedArray addObject:_dataArray [indexPath.row]];
[_deleteIndexPaths addObject:indexPath];
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([tableView isEditing]) {
if (cell.subviews.count > 3) {
if (cell.subviews[3].subviews[0]) {
((UIImageView *)(cell.subviews[3].subviews[0])).image = [UIImage imageNamed:@""];
} else {
if (cell.subviews[2].subviews[0]) {
((UIImageView *)(cell.subviews[2].subviews[0])).image = [UIImage imageNamed:@""];
}
}
}
if ([_selectedArray containsObject:_dataArray[indexPath.row]]) {
[_selectedArray removeObject:_dataArray[indexPath.row]];
[_deleteIndexPaths removeObject:indexPath];
}
}
}
左滑按钮的添加和方法的实现
//左滑添加按钮
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *likeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"喜欢" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// 实现相关的逻辑代码
// ...
// 在最后希望cell可以自动回到默认状态,所以需要退出编辑模式
NSLog(@"点击了喜欢按钮");
tableView.editing = NO;
}];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"点击了删除按钮");
[_dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}];
return @[deleteAction, likeAction];
}
语法:Swift
var listTableView: UITableView!
var dataArray:NSMutableArray!
let cellIdentifier = "cellIdentifier";
var delteteIndexPaths:NSMutableArray!
var selectArray:NSMutableArray!
2.初始化数据源和UI
func initUIAndData(){
self.navigationItem.rightBarButtonItem = self.editButtonItem()
dataArray = NSMutableArray.init(capacity: 0)
selectArray = NSMutableArray.init(capacity: 0)
delteteIndexPaths = NSMutableArray.init(capacity: 0)
listTableView = UITableView.init(frame: self.view.frame, style: .Plain);
listTableView?.delegate = self;
listTableView?.dataSource = self;
listTableView.allowsMultipleSelectionDuringEditing = true;
self.view.addSubview(listTableView!)
for i in 0...10 {
dataArray.addObject("row:\(i)")
}
let deleteButton = UIButton.init(type: .Custom)
deleteButton.frame = CGRectMake(0, CGRectGetMaxY(self.view.frame) - 40, self.view.frame.width, 40)
deleteButton .setTitle("删除", forState: .Normal);
deleteButton.backgroundColor = UIColor.orangeColor();
deleteButton .addTarget(self, action: #selector(ViewController.deleteButtonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(deleteButton)
}
3.删除方法及编辑设置
func deleteButtonClick(sender:UIButton) {
dataArray .removeObjectsInArray(selectArray as [AnyObject])
listTableView .deleteRowsAtIndexPaths(delteteIndexPaths as AnyObject as! [NSIndexPath], withRowAnimation: .Fade)
delteteIndexPaths .removeAllObjects()
selectArray.removeAllObjects()
}
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated);
if listTableView.editing {
self.editButtonItem().title = "Edit";
listTableView.setEditing(false, animated: true)
} else {
self.editButtonItem().title = "Done";
listTableView .setEditing(true, animated: animated)
}
}
4.UITableviewDatasourse的代理方法
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray!.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = cellForTableView(tableView);
cell.textLabel?.text = dataArray[indexPath.row] as? String;
return cell;
}
4.delegate代理方法
extension ViewController:UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath);
if tableView.editing {
if cell?.subviews.count > 3 {
if let _ = cell?.subviews[3].subviews[0]{
let imageView:UIImageView = cell!.subviews[3].subviews[0] as! UIImageView
imageView.image = UIImage(named:"xz");
}
}else {
if let _ = cell?.subviews[2].subviews[0]{
let imageView:UIImageView = cell!.subviews[2].subviews[0] as! UIImageView
imageView.image = UIImage(named:"xz");
}
}
}
selectArray.addObject(dataArray![indexPath.row])
delteteIndexPaths.addObject(indexPath)
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if selectArray.containsObject(dataArray[indexPath.row]) {
selectArray.removeObject(dataArray[indexPath.row])
delteteIndexPaths.removeObject(indexPath)
}
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let likeAction = UITableViewRowAction(style: .Normal, title: "喜欢", handler:{ action, indexPath in
print("点击了喜欢按钮");
tableView.editing = false
})
let deleteAction = UITableViewRowAction(style: .Default, title: "删除", handler: { action, indexPath in
self.dataArray.removeObjectAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
})
return [likeAction,deleteAction]
}
就是这样,代码已传到git上,如果你感觉有帮助就加个star吧
演示图