gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:16:58 31 4
gpt4 key购买 nike

我试图迭代 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 节
  • I=0第 0 节
  • I=8第 1 节
  • fatal error :在解包可选值时意外发现 nil值(lldb)

最佳答案

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

关于ios - TableView 循环遍历所有单元格以删除它们,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38120994/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com