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

John Baughman

macrumors regular
Original poster
Oct 27, 2003
100
0
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.
 
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.
 
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.
 
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
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.