cellForRowAtIndexPath如何工作?

时间:2011-11-10 12:24:02

标签: ios objective-c uitableview multiple-columns

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

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

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

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

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

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

3 个答案:

答案 0 :(得分:96)

我会尝试将其分解(例如来自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;

}

这是一个DataSource方法,因此无论哪个对象声明自己为DataSource的{​​{1}},都会调用它。当表视图实际需要在屏幕上显示单元格时,会根据行数和节数(在其他DataSource方法中指定)来调用它。

答案 1 :(得分:36)

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

2)NSIndexPath基本上是两件事 -

  • 你的部门
  • 你的行

由于您的表可能会划分为多个部分,每个部分都有自己的行,因此NSIndexPath将帮助您准确识别哪个部分和哪个行。它们都是整数。如果你是初学者,我会说只试一个部分。

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

至于何时有两个表视图,请查看方法。表视图传递给它,所以你不应该有这个问题。

答案 2 :(得分:6)

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

In Objective C

809px

In Swift 3.0

606px