2

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; } 
8
  • Why are you setting textLabel twice with two different values? Commented Dec 1, 2013 at 19:24
  • i was testing to see if it was accessing the dictionary correctly. i would comment one to see if the other one would appear. Commented Dec 1, 2013 at 19:26
  • So explain what's wrong with your code. What problem do you need solved in this code? You are getting row-specific data from feeds. That seems correct. Commented Dec 1, 2013 at 19:29
  • the code works fine but i want one cell to display the object with the key frstDirection and i want the other cell to display the object with the key secDirection Commented Dec 1, 2013 at 19:33
  • That makes no sense. If you have row-specific data in feeds then your values should be in the row specific data. Why is your data setup such that the data for row 0 contains values for both rows and the data for row 1 also contains values for both rows? Commented Dec 1, 2013 at 19:35

3 Answers 3

2

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"]]; } 
Sign up to request clarification or add additional context in comments.

Comments

1

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 .

Comments

1

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]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.