Problem with NSArray:

Discussion in 'iOS Programming' started by guydor, Mar 25, 2010.

  1. guydor macrumors member

    Joined:
    Mar 10, 2009
    #1
    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!
     
  2. potaco macrumors regular

    Joined:
    Mar 12, 2010
    #2
    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.
     
  3. guydor thread starter macrumors member

    Joined:
    Mar 10, 2009
    #3
    Thank you very much!

    Now I understand what I did wrong

    Thanks al lot
     
  4. guydor thread starter macrumors member

    Joined:
    Mar 10, 2009
    #4
    I have another question please:

    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!
     
  5. potaco macrumors regular

    Joined:
    Mar 12, 2010
    #5
    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).
     
  6. guydor thread starter macrumors member

    Joined:
    Mar 10, 2009
    #6
    Thanks, I'll explain my self:

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

    Thanks!
     

Share This Page