Swift 学习之 tableView 左滑 “删除”、“置顶”以及“群设置”等按钮
---恢复内容开始---
效果图:
设置这个其实很简单...只要实现 tableView 的一个代理,即:
@available(iOS 8.0, *) optional public func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? // supercedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil
看到出来,这个方法是 iOS 8.0之后才提供的
具体的使用:
@objc(tableView:editActionsForRowAtIndexPath:) func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let model: MessageMainModel = self.showList?.object(at: indexPath.row) as! MessageMainModel let list = model.editList as NSArray var mActionArray = [UITableViewRowAction]() for index in 0...list.count - 1 { let dic = list.object(at: index) as! NSDictionary let tag = dic.object(forKey: "tag") as! Int if tag == 1 { //删除 let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.destructive, title: "删除") { (deleteAction: UITableViewRowAction, indexPath: IndexPath) in self.showList?.removeObject(at: indexPath.row) self.tableView.reloadData() print("\(indexPath.row) == 删除") } deleteAction.backgroundColor = .red mActionArray.append(deleteAction) }else if tag == 2 { let setupAction = UITableViewRowAction(style: UITableViewRowActionStyle.destructive, title: "置顶") { (deleteAction: UITableViewRowAction, indexPath: IndexPath) in let model: MessageMainModel = self.showList?.object(at: indexPath.row) as! MessageMainModel self.showList?.removeObject(at: indexPath.row) self.showList?.insert(model, at: 0) self.tableView.reloadData() print("\(indexPath.row) == 置顶") } setupAction.backgroundColor = appDefaultColor mActionArray.append(setupAction) }else if tag == 3 { let groupSetting = UITableViewRowAction(style: UITableViewRowActionStyle.destructive, title: "群设置") { (deleteAction: UITableViewRowAction, indexPath: IndexPath) in print("\(indexPath.row) == 群设置") } groupSetting.backgroundColor = UIColor.lightGray mActionArray.append(groupSetting) } } return mActionArray }
---恢复内容结束---