简体   繁体   中英

UITableView Looping Out Data From NSMutableArray / NSDictionary

I'm currently building an iPhone app that will display data from an NSMutableArray called "stories". The array structure looks like so (via 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:

- (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). 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.

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.

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. So instead of your for() loop, do this:

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. 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. The 3.0 SDK introduced changes into how UITableViewCell works, including its view hierarchy. You probably want to be accessing the textLabel of the cell, and then setting the properties of that , like this:

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 setFont: [UIFont systemFontOfSize:11]];
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.

note that for a simple table with no sections, an "index path" ends up just being an array with one integer in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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