cellForRowAtIndexPath如何工作?

我已经阅读过苹果文档,对于像Objective-C这样的初学者来说,这是不可理解的。 我试图实现多列UITableView下面这个链接的例子,它只是不工作,所以我需要了解如何cellForRowAtIndexPath工作,因为我个人这个方法似乎相当复杂。

1)它返回什么? UITableViewCell ? 但为什么看起来很奇怪?

 -(UITableViewCell *)tableView:(UITableView *)tableView 
  • 那是什么? 你能解释一下吗?

2)它如何被调用,什么是更重要的,我怎么连接到一个特定的UITableView ? 如果我有两个UITableView的名为firstTableViewsecondTableView ,我希望他们是不同的(执行cellForRowAtIndexPath不同)? 我应该如何链接我的UITableViews到这个

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

该方法接受NSIndexPath ,而不是UITableView 。 我该怎么办?

1)该函数返回一个单元格的表视图是? 所以,返回的对象是UITableViewCelltypes的。 这些是您在表格行中看到的对象。 这个函数基本上返回一个单元格,用于表格视图。 但是你可能会问,函数怎么知道什么行返回什么单元格,这在第二个问题中得到了回答

2) NSIndexPath本质上是两件事 –

  • 你的部分
  • 你的排

因为你的表可能被分成许多部分,每个都有自己的行,这个NSIndexPath将帮助你精确地识别哪个部分和哪一行。 他们都是整数。 如果你是初学者,我会说只有一个部分。

如果你在你的视图控制器中实现了UITableViewDataSource协议,它会被调用。 更简单的方法是添加一个UITableViewController类。 我强烈build议这样做,因为它有一些代码可以帮助你轻松地实现可以描述表格的function。 无论如何,如果你select自己实现这个协议,你需要创build一个UITableViewCell对象,并返回它的任何行。 看看它的类的引用来理解可重用性,因为在表视图中显示的单元格被重复使用(这是一个非常有效的devisebtw)。

至于当你有两个表视图时,看看这个方法。 表视图被传递给它,所以你不应该有这方面的问题。

我会尽量分解它(例如文件 )

 /* * 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; } 

这是一个DataSource方法,所以它将在任何对象声明自己是UITableViewDataSource的时候被调用。 当表视图实际需要在屏幕上显示单元格时,将根据行和段的数目(在其他DataSource方法中指定)调用它。

基本上它正在devise你的单元格,cellforrowatindexpath是为每个单元调用的,单元号是通过indexpath.row和indexpath.rowfind的。 在这里,您可以使用标签,button或textfied图像来更新表中所有行。 第二个问题的答案在索引path中的行的单元格中使用if语句

在目标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; } 

在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 }