Susim Samanta
Susim Samanta

Reputation: 1593

Cell subviews vanishing while scrolling of uitableview

I am doing the below code. Problem is that while I am scrolling one cell is going to invisible section and when it's coming again to visible section cell's subviews are gone like label etc.

Code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"playcell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"header.png"] forState:UIControlStateNormal];
            cell.backgroundCellBtn.userInteractionEnabled = NO;
            cell.nameTextLbl.hidden = YES;
            cell.playtimeLbl.hidden = YES;
            cell.lineSperatorImageView.hidden = YES;
        }else if (indexPath.row == self.yourTurnPlayList.count +1) {
            [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"footer.png"] forState:UIControlStateNormal];
            cell.backgroundCellBtn.userInteractionEnabled = NO;
            cell.nameTextLbl.hidden = YES;
            cell.playtimeLbl.hidden = YES;
            cell.lineSperatorImageView.hidden = YES;

        }
        else {
            int pathValue = indexPath.row  -1 ;
            if (pathValue > (-1) && pathValue < self.yourTurnPlayList.count) {
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateNormal];
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateHighlighted];
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateSelected];

                GamePlay *gamePlay = [self.yourTurnPlayList objectAtIndex:pathValue];
                cell.backgroundCellBtn.tag = pathValue;
                cell.nameTextLbl.text = [self getCurrentPlayerFormattedName:gamePlay];
                cell.playtimeLbl.text = [NSString stringWithFormat:@"played %@",[Helper getLastPlayedStringFromUnixTime:gamePlay.lastMoveTime]];
            }
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"header2"] forState:UIControlStateNormal];
            cell.backgroundCellBtn.userInteractionEnabled = NO;
            cell.nameTextLbl.hidden = YES;
            cell.playtimeLbl.hidden = YES;
             cell.lineSperatorImageView.hidden = YES;
        }else if (indexPath.row == self.theirTurnPlayList.count +1) {
            [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"footer2.png"] forState:UIControlStateNormal];
            cell.backgroundCellBtn.userInteractionEnabled = NO;
            cell.nameTextLbl.hidden = YES;
            cell.playtimeLbl.hidden = YES;
            cell.lineSperatorImageView.hidden = YES;

        }
        else {
            int pathValue = indexPath.row  -1 ;
            if (pathValue > (-1) && pathValue < self.theirTurnPlayList.count) {
                 [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateNormal];
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateHighlighted];
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateSelected];
                cell.backgroundCellBtn.userInteractionEnabled = NO;
                GamePlay *gamePlay = [self.theirTurnPlayList objectAtIndex:pathValue];
                cell.nameTextLbl.text = [self getCurrentPlayerFormattedName:gamePlay];
                cell.playtimeLbl.text = [NSString stringWithFormat:@"%@",[Helper getLastPlayedStringFromUnixTime:gamePlay.lastMoveTime]];
            }
        }
    }

    return cell;
}

Upvotes: 0

Views: 369

Answers (1)

Guo Luchuan
Guo Luchuan

Reputation: 4731

Because the cell is hidden by you . When they reused , they are hidden , so you sholud set the hidden is NO , when reused

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"playcell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"header.png"] forState:UIControlStateNormal];
            cell.backgroundCellBtn.userInteractionEnabled = NO;
            cell.nameTextLbl.hidden = YES;
            cell.playtimeLbl.hidden = YES;
            cell.lineSperatorImageView.hidden = YES;
        }else if (indexPath.row == self.yourTurnPlayList.count +1) {
            [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"footer.png"] forState:UIControlStateNormal];
            cell.backgroundCellBtn.userInteractionEnabled = NO;
            cell.nameTextLbl.hidden = YES;
            cell.playtimeLbl.hidden = YES;
            cell.lineSperatorImageView.hidden = YES;

        }
        else {
            int pathValue = indexPath.row  -1 ;
            if (pathValue > (-1) && pathValue < self.yourTurnPlayList.count) {
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateNormal];
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateHighlighted];
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateSelected];

                GamePlay *gamePlay = [self.yourTurnPlayList objectAtIndex:pathValue];
                cell.backgroundCellBtn.tag = pathValue;
                cell.nameTextLbl.text = [self getCurrentPlayerFormattedName:gamePlay];
                cell.playtimeLbl.text = [NSString stringWithFormat:@"Last played %@",[Helper getLastPlayedStringFromUnixTime:gamePlay.lastMoveTime]];

               //add these
               cell.nameTextLbl.hidden = NO;
               cell.playtimeLbl.hidden = NO;
               cell.lineSperatorImageView.hidden = NO;
            }
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"header2"] forState:UIControlStateNormal];
            cell.backgroundCellBtn.userInteractionEnabled = NO;
            cell.nameTextLbl.hidden = YES;
            cell.playtimeLbl.hidden = YES;
             cell.lineSperatorImageView.hidden = YES;
        }else if (indexPath.row == self.theirTurnPlayList.count +1) {
            [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"footer2.png"] forState:UIControlStateNormal];
            cell.backgroundCellBtn.userInteractionEnabled = NO;
            cell.nameTextLbl.hidden = YES;
            cell.playtimeLbl.hidden = YES;
            cell.lineSperatorImageView.hidden = YES;

        }
        else {
            int pathValue = indexPath.row  -1 ;
            if (pathValue > (-1) && pathValue < self.theirTurnPlayList.count) {
                 [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateNormal];
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateHighlighted];
                [cell.backgroundCellBtn setBackgroundImage:[UIImage imageNamed:@"sample.png"] forState:UIControlStateSelected];
                cell.backgroundCellBtn.userInteractionEnabled = NO;
                GamePlay *gamePlay = [self.theirTurnPlayList objectAtIndex:pathValue];
                cell.nameTextLbl.text = [self getCurrentPlayerFormattedName:gamePlay];
                cell.playtimeLbl.text = [NSString stringWithFormat:@"%@",[Helper getLastPlayedStringFromUnixTime:gamePlay.lastMoveTime]];

               //add these
               cell.nameTextLbl.hidden = NO;
               cell.playtimeLbl.hidden = NO;
               cell.lineSperatorImageView.hidden = NO;
            }
        }
    }

    return cell;
}

Upvotes: 1

Related Questions