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

但是我也出现过这个错误,我也没有找出是什么问题。这是控制台日志:

Yes, that will return nil if the row isn't on screen, so force unwrapping is a bad idea. In fact calling the delegate methods yourself is a bad idea. If you want to reuse code then you should have your delegate method calla delete method. You can't retrieve all cells; you can only retrieve cells that are onscreen. You shouldn't need to retrieve all cells; you simply update your data model and have the table reflect those changes by reloading the table, reloading specific rows or inserting/deleting specific rows

– Paulw11