Andrey Chernukha Andrey Chernukha - 3 months ago 13x
iOS Question

How does cellForRowAtIndexPath work?

I HAVE READ apple documentation and it's not understandable for such a beginner in

as me. I'm trying to implement multicolumn
following this link example and it just doesn't work so i need to comprehend how
work, cause for me personally this method seems pretty complicated.

1) What does it return?
? But why does it look so odd?

-(UITableViewCell *)tableView:(UITableView *)tableView

  • What is that? Could you please explain?

2) How does it get called and what is more important how am i to connect it to a certain
??? What if i have two
's named
and i want them to be different (to perform
differently)? How am i supposed to link my
to this

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

the method accepts
, not
. What am i gonna do?


1) The function returns a cell for a table view yes? So, the returned object is of type UITableViewCell. These are the objects that you see in the table's rows. This function basically returns a cell, for a table view. But you might ask, how the function would know what cell to return for what row, which is answered in the 2nd question

2)NSIndexPath is essentially two things-

  • Your Section
  • Your row

Because your table might be divided to many sections and each with its own rows, this NSIndexPath will help you identify precisely which section and which row. They are both integers. If you're a beginner, I would say try with just one section.

It is called if you implement the UITableViewDataSource protocol in your view controller. A simpler way would be to add a UITableViewController class. I strongly recommend this because it Apple has some code written for you to easily implement the functions that can describe a table. Anyway, if you choose to implement this protocol yourself, you need to create a UITableViewCell object and return it for whatever row. Have a look at its class reference to understand re-usablity because the cells that are displayed in the table view are reused again and again(this is a very efficient design btw).

As for when you have two table views, look at the method. The table view is passed to it, so you should not have a problem with respect to that.