簡體   English   中英

如何在表視圖的底部添加按鈕以刪除多行?

[英]How to add a button to the bottom of a Table View in order to delete multiple rows?

我目前正在創建圖書館應用。 我有一個表格視圖,並且已經用一系列不同的書名設置了單元格。 我還設置了選中標記,以便用戶可以選中某些標題。 我剛剛在表格視圖的底部添加了一個按鈕,以便用戶可以選中標題,然后單擊該按鈕以將其從屏幕上刪除,就像將其簽出一樣。 但是,由於某些原因該按鈕沒有單擊,並且標題也不會從屏幕上刪除。 我在該網站上查看了許多其他回復,甚至嘗試了幾次,但似乎都沒有用。 有人能指出我正確的方向嗎? 我在下面發布我當前的代碼。

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
        }
    }

您只需要維護一個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
    }..... }

和didselect代碼:

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() }

做這些事,它將對您有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM