我一直在与人们一起测试一款应用程序,在我告诉他们可以滑动删除后,他们发现它很直观,但并不是每个人——至少以我的经验来看——都足够精明地弄明白了。
有替代方案吗?我认为理想情况下,我希望在 tableView 单元格上有一个小垃圾桶或“x”,可以按下以删除。但不确定这是否容易实现。
我遇到的第一个问题是我认为我无法将 IBOutlet
从 TableViewCell
拖到我有我的 UIViewController
表格 View 。
即使这是可能的,也不确定在单击删除按钮时我将如何实现以下功能。
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
所以只是想知道是否滑动删除是我唯一的选择?
谢谢。
最佳答案
您可以执行此操作以在 tableView 单元格上实现“x”按钮:
protocol CustomCellDelegate {
func removeButtonTappedOnCell(with indexPath: IndexPath)
}
class CustomCell: UITableViewCell {
var delegate: CustomCellDelegate?
@IBOutlet weak var removeButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
@IBAction func removeButtonTapped(_ sender: UIButton) {
let indexPath = IndexPath(row: sender.tag, section: 0)
delegate?.removeButtonTappedOnCell(with: indexPath)
}
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CustomCellDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.removeButton.tag = indexPath.row
cell.delegate = self
return cell
}
func removeButtonTappedOnCell(with indexPath: IndexPath) {
modelArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .right)
}
}
关于ios - 滑动删除是删除 tableView 行的唯一方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967515/