Labels linger in my reusable table cells

I have a table whose cells contain labels. Whenever I delete a reusable cell, the old shortcuts still linger on it. I was able to remove them using this:

    for(int a=[[newcell subviews]count]-1; a>=0;a--)
    {
        if([[[[newcell subviews]objectAtIndex:a]class] isSubclassOfClass:[UILabel class]])
        {
            [[[newcell subviews] objectAtIndex:a] removeFromSuperview];
        }
    }

But when I select a cell, I see the old text on top of the new one. I tried this:

    [[newcell.selectedBackgroundView subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
    [[newcell.backgroundView subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];

But that did not work. How can I remove old marks from a selected cell as well as the normal view of a cell?

0
source share
3 answers

, subviews cellForRowAtIndexPath: , . subview , , .

subview , (, ). , ?, .

+1

UITableViewCell ( ). prepareForReuse .

+2

I did what Yuji suggested. Instead of introducing new labels at each iteration, I checked to see if the cells contain labels, and then either edited the labels if they were there, or placed in them if they were not. The code is as follows:

    if([[newcell.contentView subviews] count]>=2 && [[[[newcell.contentView subviews] objectAtIndex:0]class] isSubclassOfClass:[UILabel class]] && 
   [[[[newcell.contentView subviews] objectAtIndex:1]class] isSubclassOfClass:[UILabel class]])
{
    //change the text of the labels
}
else
{
    //add the labels to the cell
}
0
source

All Articles