Reputation: 31
My UITableView is adding duplicate label on top of each other when I scroll up and down. So eventally so many labels get added that the add slows down and crash's.
- (UITableViewCell *)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];
}
UILabel *label;
label = [[UILabel alloc] initWithFrame:nameFrame];
[label setText:[name objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[cell addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:statusFrame];
[label setText:[status objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:(UITextAlignmentRight)];
[cell addSubview:label];
[label release];
return cell;
}
Upvotes: 3
Views: 3583
Reputation: 6093
The way I solved this was not very elegant but worked.
The problem, as mentioned by Joe is that we are reusing the cells by dequeuing them. This means that sometimes we use a cell which already has its properties previously set, eg its textLabel. In my case it was due to differences in the cell structure. It was overlapping the imageview from one cell over another which shouldn't have an image at all.
I found that identifying the problematic parts and setting them to nil at the start of cellForRowAtIndex fixed the problem. This is the equivalent of wiping your cell slate clean before reusing it.
Here is an edited version of mine:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:bCellIdentifier];
cell.imageView.image = Nil;
cell.textLabel.text = Nil;
_nameField.text = @"";
...
// Setting the code with regards to the cells here
...
return cell;
}
Hope this helps
Upvotes: 0
Reputation: 2044
You need to simply remove the subview:
for (UIView * view in cell.contentView.subviews)
{
[view removeFromSuperview];
view = nil;
}
This will do the work "I had the same issue"
Upvotes: 0
Reputation: 57179
You are dequeuing reusable cells so the UILabel already exists on each dequeued cell. Try the following code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UILabel *label;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
label = [[UILabel alloc] initWithFrame:nameFrame];
label.tag = 1; //Important for finding this label
[label setText:[name objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:statusFrame];
label.tag = 2; //Important for finding this label
[label setText:[status objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:(UITextAlignmentRight)];
[cell.contentView addSubview:label];
[label release];
}
else
{
label = (UILabel*)[cell.contentView viewWithTag:1];
label.text = [name objectAtIndex:indexPath.row + indexPath.section];
label = (UILabel*)[cell.contentView viewWithTag:2];
label.text = [status objectAtIndex:indexPath.row + indexPath.section];
}
return cell;
}
I did adjust the code to use the cell's contentView.
Upvotes: 8