【问题标题】:UITableView cellForRowAtIndexPath invoked multiple times if it's a manual call?如果是手动调用,UITableView cellForRowAtIndexPath 会被多次调用?
【发布时间】:2012-08-08 17:53:58
【问题描述】:

如果我将 UITableView 作为 UIViewController 的属性,并且我正在使用 [tableView cellForRowAtIndexPath:indexPath] 手动访问特定行的单元格。

在一个方法调用上,我应该期望看到多个调用:

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

我的每个 UITableViewCell 都有一个 UITextField,我正在尝试访问其文本数据。

这就是我正在做的事情:

for (int section = 0; ix < countOfSections; ++section)
{
    NSInteger countOfRowsInSection = // attained
    for (int row = 0; row < countOfRowsInSection; ++row)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

        // the follow call seems to elicit multiple invocations 
        // on my table delegate's
        // - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

        UITableViewCell *cell = [self.tableview cellForRowAtIndexPath:indexPath];

        // Access some custom data
    }
}

有没有更好的方法来访问存储在 UITextField 每个 UITableViewCell 中的所有部分和行的所有数据?

【问题讨论】:

    标签: ios5 uitableview


    【解决方案1】:

    您真的不应该尝试使用视图来存储数据。无论您用作表格的数据源,都应该有一个对象数组(或其他结构),其中包含在调用tableView:cellForRowAtIndexPath: 时为单元格提供内容的数据。

    如果您需要其他方式访问该信息,您应该直接从数据结构而不是显示中获取。

    【讨论】:

    • 内容由用户提供,存储在 UITableViewCell 中的 UITextField 中。我的印象是,在它们各自的 UITableViewCell 之外保留这些 UITextField 元素是很糟糕的。
    • 文本字段属于单元格,因为它们是视图的一部分,但除非您的应用程序结构非常不寻常,否则实际文本应作为应用程序数据模型的一部分进行管理。您当前的问题是一种症状,但如果您有足够的单元格需要滚动,事情就会变得非常困难。
    • 我想做的是找出一种合理的方法来捕获用户输入到 UITextField 中的文本。如果数据来自用户,那么这样做的好方法是什么?在我可以访问数据之前,我不明白如何管理数据。
    • 我会考虑注册UITextFieldDelegate 并响应事件以更新数据模型。 (代理的作用取决于应用程序设计,可能是视图控制器,也可能是UITableViewCell 的自定义子类。)
    猜你喜欢
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多