【问题标题】:UITableView Looping Out Data From NSMutableArray / NSDictionaryUITableView 从 NSMutableArray / NSDictionary 循环输出数据
【发布时间】:2009-07-21 13:34:13
【问题描述】:

我目前正在构建一个 iPhone 应用程序,该应用程序将显示来自名为“stories”的 NSMutableArray 的数据。数组结构看起来像这样(通过 NSLog):

    2009-07-20 12:38:30.541 testapp[4797:20b] (
    {
    link = "http://www.testing.com";
    message = "testing";
    username = "test";
},
    {
    link = "http://www.testing2.com";
    message = "testing2";
    username = "test2";
} )

我的 cellForRowAtIndexPath 目前看起来像这样:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];


}

    for (NSDictionary *story in stories) {
        [cell setTextColor:[UIColor whiteColor]];
        [cell setFont: [UIFont systemFontOfSize:11]];
        cell.text = [NSString stringWithFormat:[story objectForKey:@"message"]];
    }
            return cell;
}

目前我的 UITableView 显示相同项目的多个条目(恰好是数组中的最后一个集合)。如何让它成功循环遍历数组并在单元格中一个接一个地显示下一项的消息。

提前致谢:)

本吉

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview nsarray


    【解决方案1】:

    您误解了 cellForRowAtIndexPath: 方法的工作原理。

    按照您的方式,您正在创建一个单元格,然后反复重置其文本、textColor 和字体属性,然后返回一个单元格。

    理解您的问题的关键是理解 cellForRowAtIndexPath: 被调用多次,对于将在屏幕上显示的每个单元格调用一次。因此,不要使用 for() 循环,而是这样做:

    NSDictionary * story = [stories objectAtIndex:[indexPath row]];
    [cell setTextColor:[UIColor whiteColor]];
    [cell setFont: [UIFont systemFontOfSize:11]];
    cell.text = [NSString stringWithFormat:[story objectForKey:@"message"]];
    

    传入的 indexPath 参数是 tableView 如何指示它要求您使用的单元格。我们使用它从您的数组中获取相应的故事字典。

    编辑:

    我还想指出,此代码与 iPhone OS 3.0 不兼容。 3.0 SDK 对 UITableViewCell 的工作方式进行了更改,包括其视图层次结构。您可能想要访问单元格的 textLabel,然后设置 that 的属性,如下所示:

    NSDictionary * story = [stories objectAtIndex:[indexPath row]];
    [[cell textLabel] setTextColor:[UIColor whiteColor]];
    [[cell textLabel] setFont: [UIFont systemFontOfSize:11]];
    [[cell textLabel] setText:[NSString stringWithFormat:[story objectForKey:@"message"]]];
    

    【讨论】:

    • 嗨,戴夫,感谢您提供有用的帖子和 3.0 兼容性点,由于某种原因,它似乎因错误而崩溃:“*** 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因: '*** -[NSCFArray objectAtIndex:]: index (2) beyond bounds (2)'" 再次感谢。
    • @Benji 这意味着您的数组中只有 2 个项目,但 tableView 要求第三个。确保您在 numberOfRowsInSection 委托方法中返回 [stories count]。如果你是,那么确保你没有在幕后改变 NSArray 并在不告诉 tableview 的情况下添加/删除东西(你通过向它发送 reloadData 消息来告诉它)。
    • 戴夫你是个天才,我还没有返回 [故事数]。再次感谢,祝你有美好的一天:)
    【解决方案2】:

    你的问题是这样的

    for (NSDictionary *story in stories) {       
    

    [单元格 setTextColor:[UIColor whiteColor]];
    [单元格设置字体:[UIFont systemFontOfSize:11]];
    cell.text = [NSString stringWithFormat:[story objectForKey:@"message"]];
    }

    您正在将单元格设置为始终显示最后一个故事,您想要做什么

    NSDictionary *story = [stories objectAtIndex: indexPath.row];
    cell.text=[NSString stringwithFormat:[story objectForKey@"message];
    

    【讨论】:

      【解决方案3】:

      而不是

      for (NSDictionary *story in stories) {
          cell.text = [NSString stringWithFormat:[story objectForKey:@"message"]];
      }
      

      你想要的

      int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
      cell.text = [NSString stringWithFormat:[[stories objectAtIndex: storyIndex] objectForKey:@"message"]];
      

      您的 cellForRowAtIndexPath 方法将为每个单元格调用一次,因此您只想为该特定单元格设置文本。

      请注意,对于没有节的简单表,“索引路径”最终只是一个包含一个整数的数组。

      【讨论】:

      • storyIndex int 怎么了?
      • 一个月前从别人的坏例子中复制代码有罪,当时我并不真正理解 indexPaths 是什么,显然作者不知道 iPhone 特定的“行”和“部分” " 方法....
      【解决方案4】:

      这里不需要循环 - 你要做的是使用表格单元格的索引并获取数组的相应元素(不清楚,你使用的是数组还是字典?) .因此,例如,数组的第四个元素将放置在第四个表格单元格中。

      这里是修复:

       NSDictionary *story = [stories objectAtIndex: indexPath.row];
      cell.text=[NSString stringwithFormat:[story objectForKey@"message];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多