Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-09-2011, 05:17 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Question UITableView cell reuse

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!
architectpianist is offline   Reply With Quote
Old 05-10-2011, 04:19 AM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

i think the problem is here

Code:
if ([cell.contentView subviews])
		for (UIView *view in [cell.contentView subviews]) 
			[view removeFromSuperview];     //Clear the cell's subviews
you are removing all subviews, not just what you added.

To fix it, remove just the view that have a fixed tag (seem that you use section as tag for all cell)

Code:
for (UIView *view in [cell.contentView subviews]) 
		if(view.tag==indexPath.tag)	[view removeFromSuperview];
__________________
dany_dev is offline   Reply With Quote
Reply

Bookmarks

Tags
indexpath, nsdictionary, reuse, uislider, uitableviewcell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 353
9 members and 344 guests
dansparrow, iOS.Lover, lorrettaui53, MikaelBartlett, oztemel, pbart, PlutoPrime, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:58 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0