我有一个 TableView ,它是基于数组以编程方式创建的。我在按钮项上启用了编辑按钮,我希望能够从数组中删除行上的值,结果从 TableView 中删除了该值。
我有以下代码:
class TableViewController: UITableViewController {
var data = ["Apple", "Apricot", "Banana", "Blueberry", "Cantaloupe", "Cherry",
"Clementine", "Coconut", "Cranberry", "Fig", "Grape", "Grapefruit",
"Kiwi fruit", "Lemon", "Lime", "Lychee", "Mandarine", "Mango",
"Melon", "Nectarine", "Olive", "Orange", "Papaya", "Peach",
"Pear", "Pineapple", "Raspberry", "Strawberry"]
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("test", forIndexPath: indexPath)
cell.textLabel?.text = data[indexPath.row]
return cell
}
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
data.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
}
当我运行它,并单击一行上的删除按钮时,出现以下错误:
malloc: error for object 0x7ffc69f4d580: incorrect checksum for freed object - object was probably modified after being freed. set a breakpoint in malloc_error_break to debug
任何帮助将不胜感激:)
最佳答案
这是因为您在 numberOfSectionsInTableView
函数中返回了 3...
尝试只返回 1:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1 // here
}
关于ios - UITableView 删除行。 swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35894001/