Tuesday, January 12, 2016

Objective C accessing cell of a UITableView

The unique thing that cell of a UITableView is it's indexpath. This is what is passed at the time of cell preparation in cellforRowAtIndexPath delegate method. Not only in this, If the user selects any cell in the table view, this same indexpath of the cell is passed as an argument to didSelectRowAtIndexPath delegate method.

NSIndexPath's row and section properties uniquely defines the cell of a table view.


So, If we want to access a particular cell in our table view to play with it's content or it's own properties, we need to know the indexpath of the cell to access it from the table view. Once we know the indexpath of the cell, we can access it like shown below.

      UITableViewCell *myCell = [myTableView cellForRowAtIndexPath:indexpath];        

Now, we can play with our cell behaviour. Here, What if we don't have indexpath of the cell but somehow we came to know the row and section number of the cell. In this case, we need to prepare the indexpath based on the row and section values. It is as shown below.

 NSIndexPath *indexpath = [NSIndexPath indexPathForRow:rowVal inSection:sectionVal]; 

Hope this post is useful. Feel free to comment incase of any queries.