problem with getting image to show on grouped table view

Discussion in 'iOS Programming' started by geiger10d, Feb 27, 2011.

  1. geiger10d, Feb 27, 2011
    Last edited: Apr 9, 2012
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Post the header file "SettingsTableViewController.h".

    If the header doesn't define a firstSection ivar (and the evidence suggests it doesn't), then this method:
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView
    		 cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
    will not have a variable named firstSection in scope.

    The only place where you currently show a firstSection variable being declared is inside this method:
    Code:
    - (void)viewDidLoad
    
    It's declared as a local variable (the default when a variable is declared inside a method or function), so its scope is limited to that method alone.

    If you don't know the difference between a local variable and an instance variable, you should review the fundamentals of how and where variables are declared.
     
  3. geiger10d thread starter macrumors member

    Joined:
    Jan 31, 2011
    #3
    i changed the code to this
    and the image shows up when you scroll past the cell and when you scroll back its there?

    Code:
    
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    	if (cell == nil)
    	{
    		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    	}
    	
    	NSUInteger row =[indexPath row];
    NSUInteger section = [indexPath indexAtPosition:0];
    //cell.textLabel.text = [[firstSection objectAtIndex:section] objectAtIndex:row];
    	
    	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	if ([cell.textLabel.text isEqualToString:@"Email"]) {
    		[cell.imageView setImage:[UIImage imageNamed:@"email.png"]];
    		cell.detailTextLabel.text = @"Email Us";
    		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	}
    	if ([cell.textLabel.text isEqualToString:@"Follow Us"]) {
    		[cell.imageView setImage:[UIImage imageNamed:@"twitter.png"]];
    		cell.detailTextLabel.text = @"Our Twitter";
    		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	}
    	if ([cell.textLabel.text isEqualToString:@"Like Us"]) {
    		[cell.imageView setImage:[UIImage imageNamed:@"facebook.png"]];
    		cell.detailTextLabel.text = @"Our Facebook";
    		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	}
    	if ([cell.textLabel.text isEqualToString:@"Orange"]) {
    		[cell.imageView setImage:[UIImage imageNamed:@"facebook.png"]];
    		cell.detailTextLabel.text = @"Our Facebook";
    		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	}
    	if ([cell.textLabel.text isEqualToString:@"Yellow"]) {
    		[cell.imageView setImage:[UIImage imageNamed:@"facebook.png"]];
    		cell.detailTextLabel.text = @"Our Facebook";
    		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	}
    
    	[[cell textLabel] setText:contentForThisRow];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    	return cell;
    }
    
    
     
  4. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #4
    You have a bunch of if statements that set the image based on the text of the string, but you set the text of the string after you go through these image statements. So the first time you make a cell, the text won't pass any of your requirements and you don't get an image. Then the second time you setup the cell, the cell will have the previous value for text (Which may or may not be the correct value for the section/row). You should move the line to set the text to before your if statments.

    Also, if you have a class member variable called firstSection, and you use

    Code:
    NSArray *firstSection = [[NSArray alloc] initWithObjects:...];
    
    You are creating a new pointer called firstSection, and thus the class variable never gets set you should instead use your class variable

    Code:
    firstSection = [[NSArray alloc] initWithObjects:...];
    
     

Share This Page