How to create NSIndexPath for TableView in iOS?

iOSiPhone/iPadMobile Development

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)
raja
Published on 18-Jan-2019 10:30:16
Advertisements