55

I know this might sound like a dumb question, but I have looked every where. How can I do this?

I know how to do this with a swype-to-delete method, but how cam I do it outside that function?

Please post some code samples.

Thanks!
Coulton

iosfreak
  • 5,228
  • 11
  • 59
  • 102

4 Answers4

119
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

insertIndexPaths is an array of NSIndexPaths to be inserted to your table.

deleteIndexPaths is a array of NSIndexPaths to be deleted from your table.

Example array format for index paths :

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0],
        [NSIndexPath indexPathForRow:2 inSection:0],
        nil];
Sabobin
  • 4,256
  • 4
  • 27
  • 33
  • Thanks for the answer! I used your method like this: ` NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil]; [theTableView beginUpdates]; [theTableView deleteRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft]; [theTableView endUpdates];` But it still did nothing. I am thinking it has something to do with the `theTableView` part. When trying `self.tableView`, it tells me it's not a structure or union... thanks! – iosfreak Apr 07 '11 at 03:29
  • Your view controller needs to subclass UITableViewController, or if it just a UIViewController it needs to impliment UITableViewDelegate and UITableViewDataSource protocols. Then in interface builder, add a table view to your view and link its delegate and dataSource properties to your view controller. – Sabobin Apr 07 '11 at 09:38
  • I have my subclass as UIViewController with the UITableViewDelegate and DataSource (I have them linked to my UITableView in my view). What else can I be doing wrong? – iosfreak Apr 07 '11 at 21:05
  • 1
    For anyone who needs to keep their UIViewController, just make a IBOutlet, add a property in the .h, and synthesize it in your .m. Then call `self.yourTableViewOutlet` and it works. – iosfreak Apr 09 '11 at 23:03
  • 1
    Getting this error .. `Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)` – Ans Jul 17 '14 at 09:09
7

In Swift 2.1+

tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([YourIndexPathYouWantToInsertTo], withRowAnimation: .Fade)
tableView.endUpdates()

And you can create an NSIndexPath easily like so:

NSIndexPath(forRow: 0, inSection: 0)
Chris Klingler
  • 5,258
  • 2
  • 37
  • 43
4

You want these two methods: insertRowsAtIndexPaths:withRowAnimation: and deleteSections:withRowAnimation: They are both detailed in the UITableView documentation.

indragie
  • 18,002
  • 16
  • 95
  • 164
  • Thanks for the reply. Here's what I am doing: ` [processList removeObjectAtIndex:0]; [theTableView beginUpdates]; [theTableView deleteRowsAtIndexPaths:0 withRowAnimation:UITableViewRowAnimationFade]; [theTableView deleteSections:0 withRowAnimation:UITableViewRowAnimationFade]; [theTableView endUpdates];` However nothing happens. – iosfreak Apr 06 '11 at 02:07
1

Swift 4.0

    tableView.beginUpdates()
    tableView.deleteRows(at: [YourIndexPathYouWantToDeleteFrom], with: .fade)
    tableView.insertRows(at: [YourIndexPathYouWantToInsertTo], with: .fade)
    tableView.endUpdates()

For creating IndexPath use this:

IndexPath(row: 0, section: 0)
AVerguno
  • 1,277
  • 11
  • 27