2013-02-27 71 views
0

我正在做下面的代碼。問題是,當我在滾動一個單元將會無形部分,當我們再次來到可見部分細胞的子視圖都走了樣標籤等細胞subviews消失,而滾動的uitableview

代碼:

- (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; 
} 
+0

時,每個單元有代碼相同的設計?,否則使用不同的CellIdentifier對不同勢細胞 – NANNAV 2013-02-27 04:05:01

回答

1

由於電池是由你隱藏。當他們重新使用,他們是隱藏的,所以你sholud設置隱藏是NO,重複使用

- (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; 
} 
+0

細胞沒有隱藏我躲在細胞標籤的第一個和最後一個..我認爲得到它...試試 – 2013-02-27 04:21:54

+0

感謝它的工作..這是愚蠢的錯誤:( – 2013-02-27 04:33:49