簡體   English   中英

TableView遍歷所有單元格以刪除它們,Swift

[英]TableView loop through all cell for deleting them, Swift

我試圖遍歷TableView中的所有單元並刪除它們。 我調用函數DeleteAll:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var myTableView: UITableView!
    @IBAction func DeleteAll(sender: UIButton) {
        //myTableView.reloadData()
        for j in 0..<myTableView.numberOfSections {
            for i in (myTableView.numberOfRowsInSection(j) - 1).stride(through: 0, by: -1) {
                let myIndexPath = NSIndexPath(forRow: i, inSection: j)


                    print("I=" + String(i) + "section " + String(j))
                    self.tableView( myTableView, commitEditingStyle: UITableViewCellEditingStyle.Delete, forRowAtIndexPath: myIndexPath)

            }
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    var indexPathForSelectedRows = [NSIndexPath]()

    lazy var productLines: [ProductLine] = {
        return ProductLine.productLines()
    }()



    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

        let selctedRow = tableView.cellForRowAtIndexPath(indexPath)!
        if editingStyle == UITableViewCellEditingStyle.Delete {
            productLines[indexPath.section].products.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
            selctedRow.accessoryType = UITableViewCellAccessoryType.None
        }
    }

    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        let delete = UITableViewRowAction(style: .Destructive, title: "Delete") { (action, indexPath) in
            self.tableView( tableView, commitEditingStyle: UITableViewCellEditingStyle.Delete, forRowAtIndexPath: indexPath)
        }

        let share = UITableViewRowAction(style: .Normal, title: "Disable") { (action, indexPath) in

        }
        delete.backgroundColor = UIColor.blackColor()
        share.backgroundColor = UIColor.blueColor()

        return [delete, share]
    }



    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        //self.tableView( tableView, commitEditingStyle: UITableViewCellEditingStyle.Delete, forRowAtIndexPath: indexPath)

        let row = tableView.cellForRowAtIndexPath(indexPath)!
        if row.accessoryType == UITableViewCellAccessoryType.None {
            row.accessoryType = UITableViewCellAccessoryType.Checkmark
        }
        else {
            row.accessoryType = UITableViewCellAccessoryType.None
        }
    }
    // MARK: - Table view data source
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let productLine = productLines[section]
        return productLine.name
    }

    //override func tableV

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return productLines.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        let productLine = productLines[section]
        return productLine.products.count
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("uppercaseString", forIndexPath: indexPath)
        let productLine = productLines[indexPath.section]
        let product = productLine.products[indexPath.row]
        cell.textLabel?.text = product.title
        cell.detailTextLabel?.text = product.description
        cell.imageView?.image = product.image
        // Configure the cell...

        return cell
    }
}

但是我遇到了這個錯誤,並且我沒有發現什么錯誤。 這是控制台日志:

  • I = 3區0
  • I = 2區0
  • I = 1第0節
  • 我= 0第0節
  • I = 8第1節
  • 致命錯誤:解開可選值(lldb)時意外發現nil

是的,如果該行不在屏幕上,它將返回nil,因此強制展開是個壞主意。 實際上,自己調用委托方法是一個壞主意。 如果要重用代碼,則應使用委托方法calla delete方法。 您無法檢索所有單元格; 您只能檢索屏幕上的單元格。 您不需要檢索所有單元格。 您只需更新數據模型並通過重新加載表,重新加載特定行或插入/刪除特定行來使表反映這些更改

– Paulw11

暫無
暫無

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

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