使用beginUpdates / endUpdates从tableview中删除行

时间:2015-12-11 06:04:24

标签: ios swift uitableview

使用beginupdates / endupdates代替reloadData从表格视图中删除一行。

 viewTeachertblView.beginUpdates()
 let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
 self.viewTeachertblView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
 viewTeachertblView.endUpdates()

此方法不起作用,因为cellForRowAtIndexPath未被调用,indexpath在删除时失去了跟踪。

以下是完整的源代码:

extension ViewController :UITableViewDataSource,UITableViewDelegate{

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return teacherObj.count
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("teacherCell", forIndexPath: indexPath) as! TeacherTblVIewCell
        print(teacherObj[indexPath.row].valueForKey("teacher_name") as? String)
        cell.teacherName?.text = teacherObj[indexPath.row].valueForKey("teacher_name") as? String
        cell.deleteBtn.tag = indexPath.row
        cell.deleteBtn.addTarget(self, action: "deleteThisTeacher:", forControlEvents: UIControlEvents.TouchUpInside)
        return cell

    }


    func deleteThisTeacher(sender:UIButton){

        print("delete this username")
        //first delete this user name from the coredata
        print(sender.tag)
        teacherObj.removeAtIndex(sender.tag)
       // self.viewTeachertblView.reloadData()
        viewTeachertblView.beginUpdates()
        let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
        self.viewTeachertblView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
        viewTeachertblView.endUpdates()


    }

}

如何删除beginupates / endupdates的tableview行,而不是从datasource删除对象并重新加载tableview

2 个答案:

答案 0 :(得分:1)

试试这个

    func deleteThisTeacher(sender:UIButton){
         let point = viewTeachertblView.convertPoint(CGPointZero, fromView: sender)
         if let indexPath = viewTeachertblView.indexPathForRowAtPoint(point) {
            teacherObj.removeAtIndex(indexPath.row)
            viewTeachertblView.beginUpdates()
            self.viewTeachertblView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
            viewTeachertblView.endUpdates()
         }
    }

答案 1 :(得分:0)

正如rmaddy告诉他做的那样

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("teacherCell", forIndexPath: indexPath) as! TeacherTblVIewCell
        print(teacherObj[indexPath.row].valueForKey("teacher_name") as? String)
        cell.teacherName?.text = teacherObj[indexPath.row].valueForKey("teacher_name") as? String
        cell.deleteBtn.addTarget(self, action: "deleteThisTeacher:event:", forControlEvents: UIControlEvents.TouchUpInside)

        return cell

    }

    func deleteThisTeacher(sender:UIButton, event: UIEvent){

        print("delete this username")
        //first delete this user name from the coredata
        print(sender.tag)

        if let touch = event.touchesForView(sender)?.first  {
            let point = touch.locationInView(viewTeachertblView)
            if let indexPath = viewTeachertblView.indexPathForRowAtPoint(point) {

                print(indexPath.row)
                 teacherObj.removeAtIndex(indexPath.row)
                 self.viewTeachertblView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
            }
        }

    }