开发者

how to pass NSDictionary values to UITableViewCell properly

Hey guys I have a problem, I have a NSDictionary of NSArrays and im trying to set up alphabetical sections so AAAAA, BBBB, CCCC.... etc however when I pass my values over its printing out all of the A values right into one u开发者_开发百科itableviewcell then the B's in the next... which is not right what Im after. Im hoping to have alphabetical sections with one NSArray/Dictionary value per UItableviewcell..

this is how Im currently setting it up, I think maybe I might be missing an If statment however Im just not exactly sure how to do this so would like some help if possible.. thanks in advance.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    } 

    cell.accessoryType = UITableViewCellAccessoryNone; //make sure their are no tickes in the tableview 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection

    // Configure the cell...
    if(indexPath.row < [self.arraysByLetter count]){

        NSArray *keys = [self.arraysByLetter objectForKey:[self.sectionLetters objectAtIndex:indexPath.row]];
        NSString *key = [keys description];
        NSLog(@"thingy %@", key);

        cell.textLabel.text = key;
   }

    return cell;
}

This is what it looks like on the emulator

how to pass NSDictionary values to UITableViewCell properly

UPdate I made chages to my code as suggested below and now this is what happens...

when emulator first loads

how to pass NSDictionary values to UITableViewCell properly

I then scroll to the bottom of the list and back to the top and it looks like this.

how to pass NSDictionary values to UITableViewCell properly


NSString *key = [keys description];

Here you're setting the text to be the string representation of ALL the keys. :)

I think you want this (and I hope I've interpreted your code properly)

NSArray *keys = [self.arraysByLetter objectForKey:[self.sectionLetters objectAtIndex:indexPath.section]];
NSString *key = [keys objectAtIndex:indexPath.row];
NSLog(@"thingy %@", key);

So, get the KeysArray using the IndexPath section, then get the value of the cell using the IndexPath row.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜