1

The code below is for deleting and inserting cells in tableview. Deleting is working. But what is the code for inserting??

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {
        // Delete the row from the data source
        list.remove(at: indexPath.row)
        tableView.reloadData()
    }
    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
      tableView.beginUpdates()
      tableView.insertRows(at: [IndexPath(row: yourArray.count-1, section: 0)], with: .automatic)
      tableView.endUpdates()
    }    
}
Rashid KC
  • 737
  • 1
  • 9
  • 17
  • 2
    http://stackoverflow.com/questions/31870206/how-to-insert-new-cell-into-uitableview-in-swift/31870301#31870301 – Bhavin Bhadani Jan 10 '17 at 06:12
  • 1
    But it is not working in swift 3.0 @EICaptainv2.0 – Rashid KC Jan 10 '17 at 06:24
  • @KayCee please add here what you have tried while coding.It will help us understand and solve problem – Sanman Jan 10 '17 at 06:28
  • `yourarray.append([labeltext])` - What is labeltext here? @sanman – Rashid KC Jan 10 '17 at 06:33
  • `tblname.beginUpdates() tblname.insertRowsAtIndexPaths([ NSIndexPath(forRow: Yourarray.count-1, inSection: 0) ], withRowAnimation: .Automatic) tblname.endUpdates()` - beginupdates and endupdates is not there in swift 3. inserting syntax is also different I think. @sanman – Rashid KC Jan 10 '17 at 06:35
  • @KayCee yourarray.append([labeltext]) means append objects your datasource array.And then find out indexpaths for added objects and insertrows for the same. – Sanman Jan 10 '17 at 06:39
  • "labeltext" stands for ? @sanman – Rashid KC Jan 10 '17 at 06:46
  • @KayCee "labeltext" means your new object(s) to be added to your array,for which you want to insertrows. – Sanman Jan 10 '17 at 07:02

0 Answers0