Reputation: 1954
I'm going to have cells with different subviews depending on which category is chosen. I choose a new category, reload data etc, but instead of displaying new cell with other subviews, the views are being laid on each other when I switch between them categories, how to correct that? Here is my code:
//cell for row at indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
if ([currentCategory isEqualToString:@"Projects"])
{
Project *pr=[projectsArray objectAtIndex:indexPath.row];
NSLog(@"Project ID %i, ProjectName %@", pr.ident, pr.projectName);
UILabel *nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 100)];
nameLabel.text=pr.projectName;
UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 192)];
iv.image=pr.baseImage;
[cell addSubview:iv];
[cell addSubview:nameLabel];
}
else if ([currentCategory isEqualToString:@"Glossaire"])
{
Glossaire *gl=[glossaireArray objectAtIndex:indexPath.row];
UILabel *nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
nameLabel.font=[UIFont fontWithName:@"Arial" size:25.0f];
nameLabel.text=gl.glossaireName;
nameLabel.backgroundColor=[UIColor redColor];
UILabel *introLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 50, 200, 50)];
introLabel.font=[UIFont fontWithName:@"Arial" size:16.0f];
introLabel.text=gl.intro;
introLabel.backgroundColor=[UIColor redColor];
UILabel *descriptionLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 100, 350, 100)];
descriptionLabel.font=[UIFont fontWithName:@"Arial" size:16.0f];
descriptionLabel.text=gl.description;
descriptionLabel.backgroundColor=[UIColor redColor];
NSLog(@"Glossaire ID: %i, NAME: %@ INTRO: %@ Description %@", gl.ident, gl.glossaireName, gl.intro, gl.description);
[cell addSubview:nameLabel];
[cell addSubview:introLabel];
[cell addSubview:descriptionLabel];
}
return cell;
}
//And switching between categories
- (IBAction)viewProjects:(id)sender
{
currentCategory=@"Projects";
projectsArray=[dbm fetchProjectsSummary];
[mainTable reloadData];
}
- (IBAction)viewGlossaire:(id)sender
{
currentCategory=@"Glossaire";
glossaireArray=[dbm fetchGlossaireSummary];
[mainTable reloadData];
}
Also they say the reuse identifier is deprecated, what's the new version for it? thanks!
Upvotes: 0
Views: 3624
Reputation: 40995
I just answered a similar question in cellForRowAtIndexPath memory management.
Basically, cells are re-used, so you are adding your subviews to the cell each time it is displayed and they are building up over time. You could either use a different CellIdentifier for each cell layout so a cell with one layout isn't reused for a cell that needs a different layout, or you could just get rid of this logic:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
And just have this:
UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil];
That way your cells won't be re-used each time and you don't need to worry about cleaning up the contents from the last time they were used.
To actually answer your question, you could just loop through the cell subviews and say [view removeFromSuperview]
to clean them out each time, but I don't think that's a good solution.
Upvotes: 3