Mab KaaKoo
Mab KaaKoo

Reputation: 125

How to remove previous subview and add new subview to UITableviewCell

I have created cell that can be expanded and collapsed, when the cell is expanded I add 2 subviews and remove those 2 subviews when cell is collapsed. Look at the code:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(selectedIndex == indexPath.row){
        selectedIndex = -1;

        UITableViewCell *cell = [self.tblView cellForRowAtIndexPath:indexPath];
        [[cell viewWithTag:TAG_KHMER] removeFromSuperview];
        [[cell viewWithTag:TAG_KOREAN] removeFromSuperview];

        //[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [self.tblView beginUpdates];
        [self.tblView endUpdates];

        return;
    }

    if(selectedIndex >= 0){
        NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
        selectedIndex = indexPath.row;
        UITableViewCell *cell = [self.tblView cellForRowAtIndexPath:previousPath];
        [[cell viewWithTag:TAG_KHMER] removeFromSuperview];
        [[cell viewWithTag:TAG_KOREAN] removeFromSuperview];
        //[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

    VocabularyController *vc = [self.vocabularyInfo objectAtIndex:indexPath.row];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    UILabel *khmerLabel = [[UILabel alloc] init];
    khmerLabel.text = vc.khmer;
    khmerLabel.font = [UIFont fontWithName:@"Hanuman" size:17];
    [khmerLabel setNumberOfLines:0];
    khmerLabel.tag = TAG_KHMER;
    khmerLabel.frame = CGRectMake(20, 45, 300, 300);

    UILabel *koreanPro = [[UILabel alloc] init];
    koreanPro.text = vc.korean;
    [koreanPro setNumberOfLines: 0];
    koreanPro.tag = TAG_KOREAN;
    koreanPro.frame = CGRectMake(20, 315, 300, 300);

    [cell addSubview:khmerLabel];
    [cell addSubview:koreanPro];

    selectedIndex = indexPath.row;

    //[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [self.tblView beginUpdates];
    [self.tblView endUpdates];

}

What happened is the cell seem not remove the previous one. it displays new text on the old text but when I click on the same cell twice again and then the cell can render the text good.

Can anyone help me how to display it properly.

enter image description here

After click twice times on the cell.

enter image description here

Upvotes: 0

Views: 132

Answers (1)

lxt
lxt

Reputation: 31304

Don't try and add subviews like that - it's going to lead to confusion, because as you've found out UITableView recycles the cells.

Instead, create your own custom UITableViewCell subclass that can be switched between the various states you require, and has all the subviews already set up. You can do this in a number of ways - if you're using storyboards you can use prototype cells, or you can use a NIB, or you can create your custom subclass entirely in code (whichever you are most comfortable with).

Basically, don't add subviews to your cells in your table view delegate/datasource calls. Create a custom subclass, and you'll find everything much, much easier.

Upvotes: 2

Related Questions