iphone - 具有不同 TableView 标签的 TableView 的问题

标签 iphone objective-c ios uitableview

我的表格 View 有问题。让我先大致描述一下情况。我有一个顶部 2 个按钮(日历和排名)的 View 。在这两个按钮下方,我有一个表格 View 。当我按下一个按钮时,我设置了 tableview 的标签( 1 或 2 ) 并重新加载了 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 中 self 复制 2 或 3 次,但是当我刷新它们时,tableview 又恢复正常了。

我知道这是很多代码。但我希望你愿意帮助我!

谨致问候,并在此先致谢!

出现问题的屏幕

enter image description here

最佳答案

在创建新单元格的else部分添加以下行:

[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)];
}

关于iphone - 具有不同 TableView 标签的 TableView 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13741215/

相关文章:

ios - Imageview 根本没有加载

ios - 从 float 中获取字符串而不对其值进行四舍五入

iphone - 是否所有新应用程序都必须在 iPad 上运行才能通过应用程序商店审批流程?

iOS map 注释问题

Objective-C – 从应用程序委托(delegate)向 View Controller 发送消息

ios - 滚动 tableView ios swift 的末尾

ios - Watchkit 错误 : WatchKit Extension contains multiple WatchKit apps

iPhone 应用程序 : implementation of Drag and drop images in UIView

ios - UITableviewCell subview 仅在滚动或单元格重用时出现(iOS)

javascript - 为 iphone 移动网络定制的警报框