ios - UITableView 删除行。 swift 2

标签 ios swift uitableview

我有一个 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/

相关文章:

ios - 在 iOS swift 中查询使用我的应用程序的用户(解析)

swift - 使用时区转换字符串时获取错误日期

iOS - UITableViewCell 中的 UILabel 错误地截断了文本

ios - 出于回调 URL 的目的,在 Expo 中运行的 React Native 项目的 info.plist 在哪里?

ios - 在 Swift 中不使用 didSelectRowAtIndexPath 在 DetailView 中引用特定于单元格的 JSON 键

swift - Int 和 Bool 值的类型检查在 Swift 4.2 中返回不正确

ios - 如何使用 iOS8 在 UIAlertController 中设置 UITableView 的位置和大小

ios - 为 UITableView Cells 设置委托(delegate)

ios - 如何在 Swift 中传入一个 nil 通知变量

iphone - 确定第一个和最后一个 UITableViewCell