Reputation: 2395
I have an iOS project I'm working on using Xcode7 and Swift. The user can add items to an Array
called "details" that is viewed in a TableView
. Is there a way to specify the location of where you want the new item to be? Let's say I have 5 items in my array and I want the next added item to be placed in location indexPath.row
for position 3. How do I do that? How can I make the new details[indexPath.row]
be 3 for that item?
Upvotes: 0
Views: 916
Reputation: 599
You should add the item into the "details" array at a specific index, then reload the table view.
details.insert("This String", atIndex: 3)
self.tableView.reloadData()
Upvotes: 1
Reputation: 285074
Just insert the item in details
at the requested index and call tableView.reloadData()
or tableView.insertRowsAtIndexPaths:withRowAnimation:
passing the same index wrapped in [NSIndexPath]
. But first check if the index is not out of bounds.
Upvotes: 0
Reputation: 9391
Let's say details
is:
var details = ["Hello", "Hi", "Bye", "Goodbye", "No"]
Wherever you want to add the next item (I'm assuming position 3 means the 3rd index):
details.insert("Hey", atIndex: 3)
Then reload the table view:
tableView.reloadData()
Upvotes: 0