无法从tableview中删除部分和单元格

时间:2015-04-05 03:46:23

标签: swift parse-platform tableview cell sections

我正在尝试删除包含部分的tableview上的单元格,这是我得到的错误:

***断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-3318.93 / UITableView.m:1582

我使用的代码是:

override func tableView(tableView: UITableView?, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath?) {

    if (editingStyle == UITableViewCellEditingStyle.Delete) {

        let cell = tableView?.cellForRowAtIndexPath(indexPath!)
        let staffNic = cell?.textLabel?.text as String!

        var salidaStaff:PFQuery = PFQuery(className: "StaffTrabajo")
        salidaStaff.whereKeyDoesNotExist("Salida")
        salidaStaff.whereKey("Nic", equalTo: staffNic)
        salidaStaff.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!)->Void in
            if error == nil{
                for object in objects{
                    let sweet:PFObject = object as PFObject

                    // Busca el id del staff
                    let iddd:String! = sweet.objectId as String!

                    // guarda los datos de salida
                    var query = PFQuery(className:"StaffTrabajo")
                    query.getObjectInBackgroundWithId(iddd) {
                        (gameScore: PFObject!, error: NSError!) -> Void in
                        if error != nil {
                            println(error)
                        } else {
                            gameScore["Salida"] = NSDate()
                            gameScore.saveInBackground()
                                                            tableView?.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
                        }
                        self.tableView.reloadData()
                    }
                }

            }
        }
    }
}

有什么想法吗?这就是我的数据源设置方式:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.aventurasUnicas.count
}


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "\(self.aventurasUnicas.objectAtIndex(section))"
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var sectionTitle:String = aventurasUnicas.objectAtIndex(section) as String
    var sectionStaff:Array = porAventura[sectionTitle]!
    return sectionStaff.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell;

    // Obtiene el nombre del staff y lo pone en la celda
    var sectionTitle:String = aventurasUnicas.objectAtIndex(indexPath.section) as String
    var sectionStaff:Array = porAventura[sectionTitle]!
    let staffNic:String = sectionStaff[indexPath.row] as String
    cell.textLabel?.text = staffNic

    // Obtiene el subtitulo
    var subtituloAventura:String = "\(sectionTitle).\(staffNic)" as String
    var sectionStaff2:Array = subtituloTabla[subtituloAventura]!
    let staffNic2:String = sectionStaff2[0] as String
    cell.detailTextLabel?.text = staffNic2

    cell.accessoryType = UITableViewCellAccessoryType.DetailButton
    return cell
    }

1 个答案:

答案 0 :(得分:0)

对表视图的更改必须与对表视图的数据源的更改一起执行。因此,在调用deleteRowsAtIndexPaths之前,必须先修改支持表的数组。

希望在numberOfRowsInSection中,代码引用一个数组(称之为模型)并回答model.count。希望您要求表删除的indexPath.row是模型的有效索引(< model.count)。在致电deleteRowsAtIndexPaths之前,请删除indexPath.row中模型中的项目。