简体   繁体   English

UITableView从NSMutableArray / NSDictionary循环出数据

[英]UITableView Looping Out Data From NSMutableArray / NSDictionary

I'm currently building an iPhone app that will display data from an NSMutableArray called "stories". 我目前正在构建一个iPhone应用程序,该应用程序将显示来自NSMutableArray的名为“故事”的数据。 The array structure looks like so (via NSLog): 数组结构如下所示(通过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";
} )

My cellForRowAtIndexPath looks like this currently: 我的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;
}

Currently my UITableView displays multiple entries of the SAME item (which happens to be the final set in the array). 当前,我的UITableView显示SAME项的多个条目(恰好是数组中的最后一组)。 How can I get it to successfully loop through the array and display the next item's message in the cells one after the other. 我如何获取它以成功遍历数组并依次在单元格中显示下一项的消息。

Thanks in advance :) 提前致谢 :)

Benji 石磊

You're misunderstanding how the cellForRowAtIndexPath: method works. 您误会了cellForRowAtIndexPath:方法的工作方式。

The way you have it, you're creating a single cell, and then repeatedly resetting its text, textColor, and font properties, then returning a single cell. 使用它的方式是,创建一个单元格,然后反复重置其文本,textColor和font属性,然后返回一个单元格。

The key to understanding your issue is understanding that cellForRowAtIndexPath: is called multiple times , once for each cell that will be displayed on the screen. 理解您的问题的关键是理解cellForRowAtIndexPath:被多次调用,对于每个将在屏幕上显示的单元格一次。 So instead of your for() loop, do this: 因此,请执行以下操作,而不要使用for()循环:

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

The indexPath parameter being passed in is how the tableView indicates which cell it's asking you for. 传入的indexPath参数是tableView如何指示其要您选择的单元格。 We use that to grab the corresponding story dictionary from your array. 我们用它来从您的数组中获取相应的故事字典。

EDIT: 编辑:

I'd also like to point out that this code is not iPhone OS 3.0 compatible. 我还要指出,此代码与iPhone OS 3.0不兼容。 The 3.0 SDK introduced changes into how UITableViewCell works, including its view hierarchy. 3.0 SDK引入了对UITableViewCell工作方式的更改,包括其视图层次结构。 You probably want to be accessing the textLabel of the cell, and then setting the properties of that , like this: 您可能想要访问单元格的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"]]];

There is no need to have a loop here - what you want to do is to use the index of the table cell and get the corresponding element of your array (it is unclear, are you using an array or a dictionary?). 这里不需要循环-您要做的是使用表单元格的索引并获取数组的相应元素(尚不清楚,您正在使用数组还是字典?)。 So, for example, the fourth element of your array would be placed in the fourth table cell. 因此,例如,数组的第四个元素将放置在第四个表单元格中。

Here is the fix: 解决方法是:

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

Your problem here is this 你这里的问题是

for (NSDictionary *story in stories) {       

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

You are setting your cells to always display the last story, what you want to do it something like 您正在设置单元格以始终显示最后一个故事,您想要做什么,例如

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

instead of 代替

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

you want 你要

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

your cellForRowAtIndexPath method will get called once for each cell, so you only want to set the text for that particular cell. 您的cellForRowAtIndexPath方法将为每个单元cellForRowAtIndexPath一次,因此您只想设置该特定单元格的文本。

note that for a simple table with no sections, an "index path" ends up just being an array with one integer in it. 请注意,对于没有节的简单表,“索引路径”最终只是其中包含一个整数的数组。

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

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