繁体   English   中英

UITableView从NSMutableArray / NSDictionary循环出数据

[英]UITableView Looping Out Data From NSMutableArray / NSDictionary

我目前正在构建一个iPhone应用程序,该应用程序将显示来自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显示SAME项的多个条目(恰好是数组中的最后一组)。 我如何获取它以成功遍历数组并依次在单元格中显示下一项的消息。

提前致谢 :)

石磊

您误会了cellForRowAtIndexPath:方法的工作方式。

使用它的方式是,创建一个单元格,然后反复重置其文本,textColor和font属性,然后返回一个单元格。

理解您的问题的关键是理解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"]]];

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

解决方法是:

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

你这里的问题是

for (NSDictionary *story in stories) {       

[cell setTextColor:[UIColor whiteColor]];
[单元格setFont:[UIFont systemFontOfSize:11]];
cell.text = [NSString stringWithFormat:[story objectForKey:@“ message”]];
}

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

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

代替

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方法将为每个单元cellForRowAtIndexPath一次,因此您只想设置该特定单元格的文本。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM