20

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?

Adam Young
  • 1,321
  • 4
  • 20
  • 36

3 Answers3

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];
Community
  • 1
  • 1
Gil Sand
  • 5,802
  • 5
  • 36
  • 78
0

You can get indexpath using below code

NSIndexPath *index = [NSIndexPath indexPathForRow:YOURINTSTORED inSection:0];