TableView循环遍历所有单元格以删除它们,Swift

时间:2016-06-30 10:52:25

标签: ios swift uitableview

我正在尝试迭代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 = 3section 0
  • I = 2section 0
  • I = 1section 0
  • I = 0section 0
  • I = 8 section 1
  • 致命错误:在打开Optional时意外发现nil value(lldb)

1 个答案:

答案 0 :(得分:0)

  

是的,如果行不在屏幕上,那将返回nil,所以强制   展开是一个坏主意。实际上调用委托方法   你自己是一个坏主意。如果你想重用代码那么你应该有   你的委托方法马蹄莲删除方法。你无法全部检索   细胞;您只能检索屏幕上的单元格。你不应该   需要检索所有细胞;您只需更新您的数据模型即可   该表通过重新加载表,重新加载来反映这些更改   特定行或插入/删除特定行

- Paulw11