【问题标题】:How to add a button to the bottom of a Table View in order to delete multiple rows?如何在表格视图底部添加一个按钮以删除多行?
【发布时间】:2018-07-29 04:52:18
【问题描述】:

我目前正在创建一个图书馆应用程序。我有一个表格视图,并且已经设置了具有不同书名数组的单元格。我还设置了复选标记,以便用户可以选中某些标题。我刚刚在表格视图的底部添加了一个按钮,以便用户可以选中标题,然后单击该按钮以将它们从屏幕上删除,有点像检查它们。但是,由于某种原因,该按钮没有单击,并且标题也没有从屏幕上删除。我查看了该网站上的许多其他回复,我什至尝试了几个,但似乎没有一个有效。有人可以指出我正确的方向吗?我在下面发布我当前的代码。

import UIKit

class CheckOutViewController: UITableViewController {
    var books = ["The Grapes of Wrath","The Great Gatsby","Anthem","Brave New World","To Kill A Mockingbird","Of Mice And Men", "Animal Farm", "War and Peace", "Don Quixote","War and Peace","Romeo and Juliet","Gulliver's Travels"]

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return books.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = books[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark{
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
        }
        else{
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
        }
    }

    @IBAction func checkOutPressed(_ sender: Any) {
        func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
            {
                if editingStyle == .delete
                {
                    books.remove(at: indexPath.row)
                    self.tableView.reloadData()
                }
            }

            return true
        }
    }

【问题讨论】:

  • checkOutPressed 中所有错误嵌套的函数是怎么回事?
  • 抱歉,这不是我自己的代码。这是我为这个问题找到的解决方案之一,我正在尝试,但不幸的是它没有工作。
  • 发布有效代码。
  • 至少 try 以了解您正在尝试的解决方案。你知道它是如何工作的吗?
  • 这真的很奇怪,因为在这个表格视图中按钮没有点击,但是我的其他视图控制器中的按钮工作得很好,所以我不知道该怎么做。我还在试图弄清楚如何使用该按钮删除多行。

标签: ios swift uitableview button


【解决方案1】:

您只需要维护一个 indexpath 数组,例如:

    var arrTodelete = [IndexPath]()

并在 cellforIndexPath 中做一些改变:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellNew", for: indexPath)
    ....  
    cell.selectionStyle = .none
    if arrTodelete.contains(indexPath){
        cell.accessoryType = .checkmark
    }else{
        cell.accessoryType = .none
    }..... }

还有选择代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if arrTodelete.contains(indexPath){
        let index = arrTodelete.index(of: indexPath)
        arrTodelete.remove(at: index!)
    }else{
        arrTodelete.append(indexPath)
    }

    tblView.beginUpdates()
    tblView.reloadRows(at: [indexPath], with: .none)
    tblView.endUpdates()
}

最后当你点击删除按钮时:

@IBAction func checkOutPressed(_ sender: Any) {

 for ele in arrTodelete{
        books.remove(at: ele.item)
    }

    tblView.beginUpdates()
    tblView.deleteRows(at: arrTodelete, with: .fade)
    tblView.endUpdates()

    arrTodelete.removeAll()
    tblView.reloadData() }

做这些事情,它会为你工作..

【讨论】:

  • 您好,我输入了您给我的代码,但它给了我错误 Thread 1 Fatal Error: Index out of range at the arr.remove(at: ele.item).Do you know碰巧为什么会发生这种情况?
  • 只需用书籍更新 arr ...然后检查或只是看看我也更新了答案
  • 谢谢。快速提问,那个 for 语句中的 ele 是什么意思?它有什么作用?
  • 这里我们迭代 indexpath 数组,所以 ele 是一个 indexpath
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 2020-06-22
  • 2019-02-21
  • 2012-08-06
相关资源
最近更新 更多