Marnerk
Marnerk

Reputation: 33

UITableView Load Cell and ObjectForKey

I have a problem that should be quite common. I have an Array of data called taskList this comes from a JSON and has several user data. So far, so good. I make the first objectForKey:@"desc" and returns the result (Description of user) but when I try to add another objectForKey (age for example) it shows only the age :( This is the code:

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

    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    if (cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"] autorelease];
    }

    NSLog(@"%@",taskList);
    cell.textLabel.text = [[taskList objectAtIndex:indexPath.row] objectForKey:@"desc"];
    return cell;
    cell.textLabel.text = [[taskList objectAtIndex:indexPath.row] objectForKey:@"age"];
    return cell;
}

Upvotes: 0

Views: 508

Answers (2)

lnafziger
lnafziger

Reputation: 25740

do this instead:

NSString *desc = [[taskList objectAtIndex:indexPath.row] objectForKey:@"desc"];
NSString *age  = [[taskList objectAtIndex:indexPath.row] objectForKey:@"age"];

cell.textLabel.text = [desc stringByAppendingString:age];
return cell;

Another example, which formats the string (in this case the only difference is that I'm adding a space between the two but it introduces you to a very very helpful method) (and uses the two strings that we created above):

NSString *textForMyLabel = [NSString stringWithFormat:@"%@ %@", desc, age];
cell.textLabel.text      = textForMyLabel;

Or to do the same thing without the temporary variable textForMyLabel use:

cell.textLabel.text = [desc stringByAppendingFormat:@" %@", age];

Upvotes: 1

Phillip Mills
Phillip Mills

Reputation: 31016

In the code you've posted, you'll never get to the 'age' portion since it will return after setting 'desc'. Even if you fix that, you're still assigning desc and age to the same field in the cell, which isn't likely to be what you want.

Upvotes: 0

Related Questions