ios cellForRowAtIndexPath 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8079471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:18:47  来源:igfitidea点击:

How does cellForRowAtIndexPath work?

iosobjective-cuitableviewmultiple-columns

提问by Andrey Chernukha

I HAVE READ apple documentation and it's not understandable for such a beginner in Objective-Cas me. I'm trying to implement multicolumn UITableViewfollowing this linkexample and it just doesn't work so i need to comprehend how cellForRowAtIndexPathwork, cause for me personally this method seems pretty complicated.

我已经阅读了苹果文档,对于Objective-C像我这样的初学者来说是无法理解的。我正在尝试UITableView按照此 链接示例实现多列,但它不起作用,所以我需要了解如何cellForRowAtIndexPath工作,因为对我个人而言,这种方法似乎很复杂。

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

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

-(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 UITableView??? What if i have two UITableView's named firstTableViewand secondTableViewand i want them to be different (to perform cellForRowAtIndexPathdifferently)? How am i supposed to link my UITableViewsto this

2)它是如何被调用的,更重要的是我如何将它连接到某个UITableView???如果我有两个UITableView名字firstTableView并且secondTableView我希望它们不同(以cellForRowAtIndexPath不同的方式执行)怎么办?我应该如何将我的链接UITableViews到这个

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

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

该方法接受NSIndexPath,不接受UITableView。我要做什么?

采纳答案by MadhavanRP

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

1) 该函数返回表格视图的单元格是吗?因此,返回的对象的类型为UITableViewCell。这些是您在表的行中看到的对象。这个函数基本上返回一个单元格,用于表格视图。但是您可能会问,该函数如何知道要为哪一行返回哪个单元格,这在第二个问题中得到了回答

2)NSIndexPathis essentially two things-

2)NSIndexPath本质上是两件事-

  • Your Section
  • Your row
  • 你的部分
  • 你的行

Because your table might be divided to many sections and each with its own rows, this NSIndexPathwill 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.

因为您的表格可能被分成许多部分,并且每个部分都有自己的行,这NSIndexPath将帮助您准确地确定哪个部分和哪一行。它们都是整数。如果您是初学者,我会说只尝试一个部分。

It is called if you implement the UITableViewDataSourceprotocol in your view controller. A simpler way would be to add a UITableViewControllerclass. 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 UITableViewCellobject 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).

如果您UITableViewDataSource在视图控制器中实现协议,则会调用它。一种更简单的方法是添加一个UITableViewController类。我强烈推荐这个,因为 Apple 为您编写了一些代码,可以轻松实现可以描述表的功能。无论如何,如果你选择自己实现这个协议,你需要创建一个UITableViewCell对象并为任何行返回它。查看它的类参考以了解可重用性,因为表视图中显示的单元格会一次又一次地重复使用(顺便说一句,这是一个非常有效的设计)。

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.

至于什么时候有两个表视图,看方法。表视图被传递给它,所以你应该没有问题。

回答by jbat100

I'll try and break it down (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 DataSourcemethod so it will be called on whichever object has declared itself as the DataSourceof the UITableView. It is called when the table view actually needs to display the cell onscreen, based on the number of rows and sections (which you specify in other DataSource methods).

这是一种DataSource方法,因此将在任何对象已宣布自己为被称为DataSourceUITableView。根据行数和节数(您在其他 DataSource 方法中指定),当表格视图实际需要在屏幕上显示单元格时调用它。

回答by Koushik

Basically it's designing your cell, The cellforrowatindexpath is called for each cell and the cell number is found by indexpath.row and section number by indexpath.section . Here you can use a label, button or textfied image anything that you want which are updated for all rows in the table. Answer for second question In cell for row at index path use an if statement

基本上它是在设计您的单元格,为每个单元格调用 cellforrowatindexpath ,单元格编号由 indexpath.row 找到,节编号由 indexpath.section 找到。在这里,您可以使用标签、按钮或文本化图像,为表格中的所有行更新您想要的任何内容。回答第二个问题 在索引路径行的单元格中使用 if 语句

In Objective C

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

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