iphone - 用于表视图的cellForRowAtIndexPath到底是什么时候被调用的

时间:2010-02-01 15:30:36

标签: iphone uitableview

我有一个关于调用cellForRowAtIndexPath的基本(noob)问题?

我正在使用示例代码,我没有看到它明确地在任何地方调用?

UITableViewComponent类型的任何组件在创建时是否会自动调用此函数?

由于

2 个答案:

答案 0 :(得分:5)

当UITableView显示时,每行都会调用此方法。在其中,您将使用特定数据自定义每个单元格以供显示。

这是class reference

以下是一个示例:

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

    static NSString *CellIdentifier = @"FriendCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    NSUInteger row = [indexPath row];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    Item *i = [itemArray objectAtIndex:row];
    cell.textLabel.text = [i name];
    cell.detailTextLabel.text = [i brewery];
    [i release];
    return cell;
}

答案 1 :(得分:1)

在绘制相应的UITableView时,会为每个单元格调用此方法。传入索引路径,并根据部分和单元格编号,您的代码应生成要在UI中显示的UITableViewCell。

您可以查看here以获取有关UITableViews如何工作的快速教程。