Problem with if statements

Discussion in 'iOS Programming' started by John Baughman, Feb 11, 2010.

  1. John Baughman macrumors member

    Joined:
    Oct 27, 2003
    #1
    Perhaps someone can point out what I am doing wrong...

    Code:
       if (indexPath.section == 0) {
    	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	cell.textLabel.text = [travelerInfoLabel objectAtIndex:indexPath.row];
    	cell.detailTextLabel.text =[travelerInfo objectAtIndex:indexPath.row];
    		
       }else if (indexPath.section == 1){
    	if(indexPath.row != 2) {
    		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    	}
    	cell.textLabel.text =[travelerHomeInfoLabel objectAtIndex:indexPath.row];
    	cell.detailTextLabel.text =[travelerHomeInfo objectAtIndex:indexPath.row];
    		
       }else if (indexPath.section == 2) {
    	if(indexPath.row == 1) {
    		UISlider *mySlider = [[UISlider alloc] init];
    		mySlider.maximumValue = 9;
    		mySlider.minimumValue = 0;
    		mySlider.continuous = YES;
    		mySlider.value = [[travelerSleepCycle objectAtIndex:indexPath.row] floatValue];
    		cell..accessoryView = mySlider;
    		[mySlider addTarget:self action:@selector(sleepSliderChanged:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
    		[mySlider release];
    		cell.detailTextLabel.text =[NSString stringWithFormat:@"%@hrs ", [travelerSleepCycle objectAtIndex:indexPath.row]];
    			
    	}else {
    		cell.detailTextLabel.text =[travelerSleepCycle objectAtIndex:indexPath.row];
    	}
    
    	cell.textLabel.text =[travelerSleepCycleLabel objectAtIndex:indexPath.row];
       }
    
    
    When I execute the above code in my cellForRowAtIndexPath method, the slider is properly displayed in section 2 row 1 but is also showing up in Section 0 row 1. Also the accessoryDisclosure Indicator is showing up in Section 2 row 1, but I did not ask for one there.
     
  2. John Baughman thread starter macrumors member

    Joined:
    Oct 27, 2003
    #2
    I just noticed that the slider does not show in section 0 row 1 until I scroll it out of view and down to where the slider is in section 2 row 1 is visible. When I scroll back to make section 0 row 1 visible, the slider is there.
     
  3. Troglodyte macrumors member

    Joined:
    Jul 2, 2009
    #3
    It's because cells are reused so you cannot assume you start with them 'empty'.

    BTW, check out the switch statement. It saves all that if else if nonsense.
     
  4. John Baughman thread starter macrumors member

    Joined:
    Oct 27, 2003
    #4
    Thanks Troglodyte. I put cell.accessoryView = nil; just before the if statements and that fixed the problem.

    Yes I know I can and probably should use a switch statement, but I tend to shy away from them as they tend to confuse me for some reason. The nested if statements may not be pretty but they work and I find them easier to follow.

    John
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    You might want to consider defining the slider in the (cell == nil) section, as well as probably using a different cell identifier for those cells.
     

Share This Page