Deleting a row from tableview using beginupdates
/endupdates
instead of reloadData
.
viewTeachertblView.beginUpdates()
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
self.viewTeachertblView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
viewTeachertblView.endUpdates()
This method doesnot work as cellForRowAtIndexPath
is not called and indexpath
gets out of track while deleting.
Here is the full source code:
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()
}
}
How to delete tableview row the beginupates
/endupdates
instead of deleting object from the datasource
and reloading tableview