Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Cell IndexPath

Can anyone tell me how can I get a cell IndexPath? I was saving the IndexPath in the tableView: cellForRowAtIndexPath: method but this only loads when the cell is viewed and I need to know its index path on the viewDidLoad method. this resulted in a null value because as I said it only loads its value after the cell was viewed once.

Thank you.

EDIT

The intention is being able to jump to a specific cell that has its specific number but its not linear with the sections and rows count.

like image 816
Leonardo Marques Avatar asked Mar 30 '10 19:03

Leonardo Marques


People also ask

How do I get IndexPath in cell Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What is IndexPath section?

Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section. For example, the first row in a table would have section 0, row 0, whereas the eighth row in the fourth section would have section 3, row 7.

How do I add a prototype cell to UITableView?

First, design your table cell on Storyboard whatever you want. Now, make a subclass of UITableViewCell and assign this class to your prototype custom cell which you have designed on Storyboard. Also, don't forget to set "reuse identifier in Storyboard table cell.


2 Answers

Use the method indexPathForCell: on the tableView, given a pointer to a UITableViewCell it returns its indexPath.

You can put the scrollToRowAtIndexPath:atScrollPosition:animated: in the viewWillAppear method if it is not set up yet in viewDidLoad. This is a better place anyway if the view can appear more than once after being loaded, such as when a modal view controller it invokes resigns.

like image 56
progrmr Avatar answered Oct 23 '22 10:10

progrmr


A UITableViewCell doesn't have an NSIndexPath property. The UITableViewDataSource delegate defines methods that ask for a cell for a given NSIndexPath, but that doesn't mean the cell has any enduring relationship to an NSIndexPath property.

Maybe it would help if you explained more of what you wanted this property for? What do you want to do with the cell's location in viewDidLoad?

like image 23
pkananen Avatar answered Oct 23 '22 12:10

pkananen