从TableViewController中删除单元格

时间:2015-09-08 10:07:06

标签: swift tableview

我想从tableViewController中删除单元格,但问题是删除下一个单元格 示例: 数组:[1,2,3,4,5] 当我从表中删除2时,自动将代码从DataBase中删除3

这是我的代码:

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        array_prodacts.removeAtIndex(indexPath.row) 
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! TableViewCell2 }}

1 个答案:

答案 0 :(得分:1)

问题出在array_prodacts.removeAtIndex(indexPath.row),因为tableview是基于零的索引,因此数组中元素1的indexPath为0,2为1,3是2,4是3因此,删除索引2反映了删除数组中的值3(具有索引2)

相关问题