I am making a basic quiz application.The choices are in the form of a table view. If the user moves to a previous question, I want to show him his previously selected choice. I have stored the choice in NSUserDefaults. How do I convert this int value to NSIndexpath and use it in my tableview?
Asked
Active
Viewed 3.2k times
3 Answers
65
See the NSINdexPath UIKit Additions
In your case you can use (Swift):
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
Swift 3.0:
let indexPath = IndexPath(row: 0, section: 0)

zisoft
- 22,770
- 10
- 62
- 73
4
Duplicate of This post
I copied the recieved answer here
For an int index:
NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];
Creates Index of the item in node 0 to point to as per the reference.
To use the indexPath in a UITableView, the more appropriate method is
NSIndexPath *path = [NSIndexPath indexPathForRow:yourInt inSection:section];
-
3This is ObjectiveC, not Swift – zisoft Apr 03 '15 at 07:42
-
1Fair enough, but it works the same. You can suggest an edit to my post with the swift code version. I bet there is an identical method ;) – Gil Sand Apr 03 '15 at 07:43
-
1He might as well post his own answer. – Josh Jun 16 '15 at 15:28
0
You can get indexpath using below code
NSIndexPath *index = [NSIndexPath indexPathForRow:YOURINTSTORED inSection:0];

Aman_Neuron
- 11