2012-12-06 98 views
0

我有我的tableview問題。首先讓我描繪一下情況。我有一個頂部2 buttons(日曆和排名)的看法。在這兩個按鈕下面我有一個tableview。當我按下按鈕時,我設置了tableview's tag (1 or 2)並重新加載了tableview的數據。與不同tableview標籤的tableview問題

在我的排名中,我將一個特定單元格的文字顏色設爲藍色。但是當我刷新時,其他單元格的文本變藍。

現在爲我的cellForRowAtIndex我有很多的代碼。因爲我不知道問題出在哪裏,所以我會在這裏發佈所有的代碼。我希望你不要打擾。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //standard cell 
    static NSString *simpleTableIdentifier = @"KalenderCell"; 
    KalenderCell *cell = (KalenderCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if ((cell == nil) || (![cell isKindOfClass:KalenderCell.class])) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"KalenderCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    //Cells for button 2 
    if(self.tableView.tag == 2){ 
     static NSString *simpleTableIdentifier = @"KlassementCell"; 

     KlassementCell *cell = (KlassementCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
     if ((cell == nil) || (![cell isKindOfClass: KlassementCell.class])) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"KlassementCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
     } 

     Klassement *klassement = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
     NSData *dataIcon = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:klassement.icon]]; 
     UIImage *imgIcon = [UIImage imageWithData:dataIcon]; 


     cell.lblPosition.text  = klassement.position; 
     cell.lblName.text   = klassement.name; 
     cell.lblgamesPlayed.text = klassement.gamesPlayed; 
     cell.lblGamesWon.text  = klassement.gamesWon; 
     cell.lblGamesTied.text  = klassement.gamesTied; 
     cell.lblGamesLost.text  = klassement.gamesLost; 
     cell.lblGoalsPos.text  = klassement.goalsPos; 
     cell.lblGoalsNeg.text  = klassement.goalsNeg; 
     cell.lblGoalsDiff.text  = [NSString stringWithFormat:@"%@", klassement.goalsDiff]; 
     cell.lblPoints.text   = klassement.points; 
     cell.imgClub.image   = imgIcon; 

     if([klassement.name isEqualToString:@"KRC Genk"]){ 
      cell.lblPosition.textColor  = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      cell.lblName.textColor   = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      cell.lblgamesPlayed.textColor = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      cell.lblGamesTied.textColor  = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      cell.lblGamesLost.textColor  = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      cell.lblGoalsDiff.textColor  = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      cell.lblGoalsNeg.textColor  = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      cell.lblGoalsPos.textColor  = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      cell.lblPoints.textColor  = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      cell.lblGamesWon.textColor  = [UIColor colorWithRed:(56/255.0) green:(119/255.0) blue:(189/255.0) alpha:100] ; 
      return cell; 
     } 

     return cell; 

     //Cells for button 1 
    }else if(self.tableView.tag == 1){ 
     static NSString *simpleTableIdentifier = @"KalenderCell"; 

     KalenderCell *cell = (KalenderCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
     if ((cell == nil) || (![cell isKindOfClass:KalenderCell.class])) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"KalenderCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
     } 

     Kalender *kalender = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

     NSData *imgDataHome = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:kalender.homeIcon]]; 
     NSData *imgDataAway = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:kalender.awayIcon]]; 

     UIImage *imgHome = [UIImage imageWithData:imgDataHome]; 
     UIImage *imgAway = [UIImage imageWithData:imgDataAway]; 

     // Then configure the cell using it ... 
     if([kalender.type isEqualToString:@"JPL"]){ 
      _imgType = [UIImage imageNamed:@"jupiler.png"]; 
     }else if ([kalender.type isEqualToString:@"EU"]){ 
      _imgType = [UIImage imageNamed:@"europa.jpg"]; 
     }else { 
      _imgType = nil; 
     } 

     cell.lblHome.text   = kalender.home; 
     cell.lblHomeScore.text  = kalender.homeScore; 
     cell.lblAwayScore.text  = kalender.awayScore; 
     cell.lblAway.text   = kalender.away; 
     cell.lblDate.text   = kalender.date_text; 
     cell.lblHour.text   = kalender.hour; 
     cell.img_Home.image   = imgHome; 
     cell.img_Away.image   = imgAway; 
     cell.img_type.image   = _imgType; 


     return cell; 
    } 


    return cell; 

} 

的另一個問題是,有時細胞複製自身的tableview內2〜3次,但是當我刷新他們泰伯維回來確定。

我知道這是很多代碼。但我希望你想幫助我!

親切的問候,並提前感謝!

屏上用波紋管條件的問題

enter image description here

+0

只是刪除所有的子視圖每次使用dequeueReusableCellWithIdentifier加載單元格時,都會顯示單元格的內容。它真的適合我。在下面試試我的答案。 –

回答

1

設置藍色

if([klassement.name isEqualToString:@"KRC Genk"]){ 
     /// set blue color 
    } 

,因爲它與黑色

像波紋管添加其他部分.. 。

  else{ 
      cell.lblPosition.textColor  = [UIColor blackColor] ; 
      cell.lblName.textColor   = [UIColor blackColor] ; 
      cell.lblgamesPlayed.textColor = [UIColor blackColor] ; 
      cell.lblGamesTied.textColor  = [UIColor blackColor] ; 
      cell.lblGamesLost.textColor  = [UIColor blackColor] ; 
      cell.lblGoalsDiff.textColor  = [UIColor blackColor] ; 
      cell.lblGoalsNeg.textColor  = [UIColor blackColor] ; 
      cell.lblGoalsPos.textColor  = [UIColor blackColor] ; 
      cell.lblPoints.textColor  = [UIColor blackColor] ; 
      cell.lblGamesWon.textColor  = [UIColor blackColor] ; 
      return cell; 
     } 
+0

這不起作用,其他細胞越來越藍。 – Steaphann

+0

我告訴問題,當你滾動tableview時,你的單元格與另一個單元格一起變成藍色,這是什麼問題? –

+0

不,我有一個拉下來刷新我的tableview功能。當這稱爲我清空我的tableview的數據,然後重新加載它。重新加載時。除'KRC GENK'細胞外的其他細胞都是藍色的。 – Steaphann

2

添加以下行創建新小區的其他部分:

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

你可以看到我的回答here

就試試這個:

if ((cell == nil) || (![cell isKindOfClass:KalenderCell.class])) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"KalenderCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
else 
{ 
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
}