Hi,
I'm using a table view to show RGB color sliders and a preview color (4 cells per section). I have an NSMutableArray containing as many NSDictionaries as there are sections, and each section contains sliders for the keys @"Red", @"Green", and @"Blue". I'm getting some very strange effects on the cells after scrolling. As soon as I leave some cells to scroll down, their sliders disappear, but I can still interact with them! This is the code I'm using:
Code:
-(void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if ([cell.contentView subviews])
for (UIView *view in [cell.contentView subviews])
[view removeFromSuperview]; //Clear the cell's subviews
cell.textLabel.text = @"";
if (indexPath.row == 0)
{
cell.textLabel.text = @"Red";
if ([[sliders objectAtIndex:indexPath.section] objectForKey:@"Red"])
{
[cell.contentView addSubview:[[sliders objectAtIndex:indexPath.section] objectForKey:@"Red"]];
}
else
{
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(80, 0, 196, 50)];
slider.minimumValue = 0.0;
slider.maximumValue = 1.0;
slider.tag = indexPath.section;
const CGFloat *colors = CGColorGetComponents([[self.newColors objectAtIndex:indexPath.section] CGColor]);
slider.value = colors[0];
[cell.contentView addSubview:slider];
[slider addTarget:self action:@selector(changeRed:) forControlEvents:UIControlEventValueChanged];
[[sliders objectAtIndex:indexPath.section] setObject:slider forKey:cell.textLabel.text];
}
}
//etc. etc.
else if (indexPath.row == 3)
{
if ([previewers objectForKey:[NSString stringWithFormat:@"%i", indexPath.section]])
{
NSLog(@"%i %i is already present.", indexPath.section, indexPath.row);
[cell.contentView addSubview:[previewers objectForKey:[NSString stringWithFormat:@"%i", indexPath.section]]];
}
else
{
VSColorPreviewer *previewer = [[[VSColorPreviewer alloc] initWithFrame:CGRectMake(5, 5, 270, 34)] autorelease];
previewer.backgroundColor = [UIColor clearColor];
previewer.lineColor = [self.newColors objectAtIndex:indexPath.section];
[cell.contentView addSubview:previewer];
[previewers setObject:previewer forKey:[NSString stringWithFormat:@"%i", indexPath.section]];
}
}
return cell;
}
Any help would be appreciated!