How does cellForRowAtIndexPath work?

I read the documentation for the apple, and this is not clear for a newbie to Objective-C like me. I am trying to implement a multi- UITableView by following this link and it just does not work, so I need to understand how cellForRowAtIndexPath works, for me personally this method seems rather complicated.

1) What does he return? UITableViewCell ? But why does it look so weird?

 -(UITableViewCell *)tableView:(UITableView *)tableView 
  • What is it? Could you explain?

2) How is it called, and more importantly, how to connect it to a specific UITableView ? What if I have two UITableView named firstTableView and secondTableView and I want them to be different (to execute cellForRowAtIndexPath differently)? How should I associate my UITableViews with this

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

The method accepts NSIndexPath , not a UITableView . What will i do?

+48
ios objective-c uitableview multiple-columns
Nov 10 2018-11-11T00:
source share
3 answers

1) The function returns a cell to represent the table yes? Thus, the returned object is of type UITableViewCell . These are the objects that you see in the rows of the table. This function basically returns a cell to represent the table. But you may ask how the function will know which cell will return for which row, what the second question answers

2) NSIndexPath essentially two things -

  • Your section
  • Your line

Since your table can be divided into many sections and each with its own rows, this NSIndexPath will help you determine exactly which section and which row. They are integers. If you are a beginner, I would say try only one section.

It is called if you implement the UITableViewDataSource protocol in your view controller. An easier way would be to add the UITableViewController class. I highly recommend this because Apple has code written for you to easily implement features that can describe a table. In any case, if you decide to implement this protocol yourself, you need to create a UITableViewCell object and return it for any row. Take a look at its cool link to understand re-usablity, because the cells displayed in the table view are reused over and over again (this is a very efficient btw project).

As for when you have two kinds of tables, look at the method. The view of the table is passed to it, so you should not have problems with this.

+37
Nov 10 '11 at 12:50
source share

I will try to break it (example from documention )

 /* * The cellForRowAtIndexPath takes for argument the tableView (so if the same object * is delegate for several tableViews it can identify which one is asking for a cell), * and an indexPath which determines which row and section the cell is returned for. */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { /* * This is an important bit, it asks the table view if it has any available cells * already created which it is not using (if they are offScreen), so that it can * reuse them (saving the time of alloc/init/load from xib a new cell ). * The identifier is there to differentiate between different types of cells * (you can display different types of cells in the same table view) */ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; /* * If the cell is nil it means no cell was available for reuse and that we should * create a new one. */ if (cell == nil) { /* * Actually create a new cell (with an identifier so that it can be dequeued). */ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } /* * Now that we have a cell we can configure it to display the data corresponding to * this row/section */ NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row]; cell.textLabel.text = [item objectForKey:@"mainTitleKey"]; cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"]; NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"]; UIImage *theImage = [UIImage imageWithContentsOfFile:path]; cell.imageView.image = theImage; /* Now that the cell is configured we return it to the table view so that it can display it */ return cell; } 

This is a DataSource method, so it will be called on which object declared itself as a DataSource UITableView . It is called when the table view actually needs to display a cell on the screen, depending on the number of rows and partitions (which you specify in other DataSource methods).

+92
Nov 10 '11 at
source share

Basically this is the design of your cell, Cellforrowatindexpath is called for each cell, and the cell number is found by indexpath.row and the section number by indexpath.section. Here you can use a shortcut, button or text image, anything that is updated for all rows of the table. The answer to the second question In the cell for the row along the index path, use the if statement

In Objective C

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(tableView == firstTableView) { //code for first table view [cell.contentView addSubview: someView]; } if(tableview == secondTableView) { //code for secondTableView [cell.contentView addSubview: someView]; } return cell; } 

In Swift 3.0

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell! if(tableView == firstTableView) { //code for first table view } if(tableview == secondTableView) { //code for secondTableView } return cell } 
+6
May 8 '15 at 9:58
source share



All Articles