繁体   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