How to create NSIndexPath for TableView in iOS?


Index path is generally a set of two values representing row and section of a table view. Index path can be created in objective C as well as Swift as both are native language of iOS Development.

IndexPathForRow is a class method in iOS. To create a index path we need to be sure about the section and row we need to create. Below are the methods of creating an Indexpath.

To create an IndexPath in objective C we can use.

NSIndexPath *myIP = [NSIndexPath indexPathForRow: Int inSection:Int] ;

Example

NSIndexPath *myIP = [NSIndexPath indexPathForRow: 5 inSection: 2] ;

To create an IndexPath in Swift we can use.

IndexPath(row: rowIndex, section: sectionIndex)

Example

IndexPath(row: 2, section: 4)

Both of these are generally used in Cell for row at method, which can be used as

let cell = tblView.cellForRow(at: IndexPath(row: 5, section: 2)

Updated on: 30-Jul-2019

708 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements