Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

guydor

macrumors member
Original poster
Mar 10, 2009
67
0
Hi,
I have a NSArray and an UITableViewCell.
In my NSArray I have 4 objects which I want them into the UITableViewCell with 4 images near each cell.
From some reason, in the UITableViewCell, only the fourth and the last image appears. What should I do?

*The UITableViewCell is actually UIPopoverController

Here's my code:
Code:
- (void)viewDidLoad {
    [super viewDidLoad];
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
	
	self.myArray = [NSArray arrayWithObjects:@"Object One", @"Object Two", @"Object Three", @"Object Four", nil];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CellIdentifier";
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
	cell.textLabel.text = [self.myArray objectAtIndex:indexPath.row];
	
	
	if ([myArray objectAtIndex:0]) {
		UIImage *googleIcon = [UIImage imageNamed:@"Image 0.png"];
		cell.imageView.image = googleIcon;
	}
	
	if ([myArray objectAtIndex:1]) {
		UIImage *yahooIcon = [UIImage imageNamed:@"Image 1.png"];
		cell.imageView.image = yahooIcon;
	}
	
	if ([myArray objectAtIndex:2]) {
		UIImage *bingIcon = [UIImage imageNamed:@"Image 2.png"];
		cell.imageView.image = bingIcon;
	}
	
	if ([myArray objectAtIndex:3]) {
		UIImage *askIcon = [UIImage imageNamed:@"Image 3.png"];
		cell.imageView.image = askIcon;
	}
	
	
	return cell;
}

}

Thanks!
 

potaco

macrumors regular
Mar 12, 2010
152
4
I'm not totally familiar with Obj-C since I'm just getting started myself... but isn't each "if" overriding the value of cell.imageView.image?

The way I interpret it:

- You have an array (myArray) with four objects (Object One through Object Four)
- For a cell, you check whether the array has at object at each location and set the image value.
- Each "if" overrides the image value set in the previous "if".
- Since you have 4 objects, all four "if"s will always be true and the last one will always be used.

Basically, it looks like you need a way to assign the image based on the specific cell you're dealing with, not just whether things exist in the array.
 

guydor

macrumors member
Original poster
Mar 10, 2009
67
0
Thank you very much!

Now I understand what I did wrong

Thanks al lot
 

guydor

macrumors member
Original poster
Mar 10, 2009
67
0
I have another question please:

I'm not totally familiar with Obj-C since I'm just getting started myself... but isn't each "if" overriding the value of cell.imageView.image?

The way I interpret it:

- You have an array (myArray) with four objects (Object One through Object Four)
- For a cell, you check whether the array has at object at each location and set the image value.
- Each "if" overrides the image value set in the previous "if".
- Since you have 4 objects, all four "if"s will always be true and the last one will always be used.

Basically, it looks like you need a way to assign the image based on the specific cell you're dealing with, not just whether things exist in the array.

How do I create an "if" command which the mining of it in simple words:
if this row is selected --> do this.
and if this row is selected --> do that

?

Thanks!
 

potaco

macrumors regular
Mar 12, 2010
152
4
Don't use the exact syntax below because I promise it won't work, but I think it would be something like...

Code:
cell.textLabel.text = [self.myArray objectAtIndex:indexPath.row];
	
	if ([myArray objectAtIndex:indexPath.row] == "Object One") {
		UIImage *googleIcon = [UIImage imageNamed:@"Image 0.png"];
		cell.imageView.image = googleIcon;
	}

... etc

Essentially, you're checking WHAT object is in a specific array location, not just that an object exists there. Use the indexPath.row to check that location in the array. If the object there is Object One, set the image to Image 0. If it's Object Two, set the image to Image 1, etc. I'm sure there's a better way to code it, so maybe someone else can provide some more specific info.

Also, I'd probably change the subsequent "if"s to "else if"s (if the first one matches, there's no reason to check the others).
 

guydor

macrumors member
Original poster
Mar 10, 2009
67
0
Thanks, I'll explain my self:

Don't use the exact syntax below because I promise it won't work, but I think it would be something like...

Code:
cell.textLabel.text = [self.myArray objectAtIndex:indexPath.row];
	
	if ([myArray objectAtIndex:indexPath.row] == "Object One") {
		UIImage *googleIcon = [UIImage imageNamed:@"Image 0.png"];
		cell.imageView.image = googleIcon;
	}

... etc

Essentially, you're checking WHAT object is in a specific array location, not just that an object exists there. Use the indexPath.row to check that location in the array. If the object there is Object One, set the image to Image 0. If it's Object Two, set the image to Image 1, etc. I'm sure there's a better way to code it, so maybe someone else can provide some more specific info.

Also, I'd probably change the subsequent "if"s to "else if"s (if the first one matches, there's no reason to check the others).

I want to assign an action to each row in the UITableViewCell,
Action will be active only when you tap the row.

Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.