2012-12-06 94 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)]; 
}