Troy Barrios
Troy Barrios

Reputation: 177

Populating a table view cell

I currently have an dictionary containing 2 objects with separate keys, and I want to populate a table view cell with each object respectively. So say for example my dictionary has 2 objects one with the key north and another object with the key south. Now I want the table view to have 2 cells one containing north and one containing south. How would I go about doing that? so far i tried the code below but that only overrides it.

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

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
    [cell.textLabel setText:[news objectForKey:@"frstDirection"]];
    [cell.textLabel setText:[news objectForKey:@"secDirection"]];

    return cell;
}

Upvotes: 2

Views: 133

Answers (3)

dispatchMain
dispatchMain

Reputation: 1617

If you are sure you have only two objects, you simply can use this:

NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
if(indexPath.row==0){
     [cell.textLabel setText:[news objectForKey:@"frstDirection"]];
}else if(indexPath.row==1){
    [cell.textLabel setText:[news objectForKey:@"secDirection"]];
}

Upvotes: 2

megha
megha

Reputation: 84

In your case, you are trying to set value for cell twice so every time last value you will get as uitableviewcell.

So try following code before [cell.textLabel setText:[news objectForKey:@"frstDirection"]];

NSMutableString *teststring = [NSMutableString string];
[teststring appendString:[news objectForKey:@"frstDirection"]];
[teststring appendString:[news objectForKey:@"secDirection"]];
[cell.textLabel setText:teststring];

Upvotes: 1

ManiaChamp
ManiaChamp

Reputation: 857

put if condition , if(indexPath.row % 2 == 0) then first direction else second direction. In this way you will alternate cells with first then second .

Upvotes: 1

Related Questions