TableViewCell不按预期方式重新加载

问题描述:

我有一个小应用程序,可以跟踪玩家在体育比赛中玩的时间。我有一个体育比赛列表,你可以点击进入并开始跟踪比赛细节。我试图做到这一点,如果游戏正在进行并且用户返回到游戏列表,那么他们不能点击另一个游戏单元格,因为它会覆盖活动游戏中的所有当前数据。TableViewCell不按预期方式重新加载

我几乎完成了这项工作。当游戏进行时,我回到体育游戏列表中,只有活动游戏可以访问,并向用户显示它是活跃的。但是当我回去重新设置游戏时,我期望所有体育游戏的tableView都可以访问。但他们不是。它仍然只显示一个活动的游戏,其他所有游戏都无法访问。我在viewWillAppear中使用tableView.reloadData。我也在下面显示了相关的代码。

// gameViewController -> shows all the games you can track 
override func viewWillAppear(_ animated: Bool) { 
    self.tableView.reloadData() 
    for game in fetchedResultsController.fetchedObjects! { 
     print("Game Opposition is \(game.opposition)") 
     print("Is Playing? \(game.isPlaying)") 
     print("Current Playing Time \(game.currentGameTime)") 
     print("----------------------") 
    } 
} 

// game cell view controller -> checks to see if any games are in progress 
func isAnyGamesInProgress(games: [Game]) -> Bool { 
    let inProgressGames = games.filter({ Int($0.currentGameTime) > 0 && $0.gameComplete == false }) 
if inProgressGames.isEmpty { 
    return false 
} 
return true 
} 

// configures the games cells in tableview 
func configureFixtureCell(fixture: Game){ 
oppositionLabel.text = "\(fixture.team.name) v \(fixture.opposition)" 

// check if any games are currently in progress, if so disable all the other cells 
// this prevents a user being mid game and checking an old game and inadvertently updating all the played time values 
let games = fixture.team.game?.allObjects as! [Game] 

if isAnyGamesInProgress(games: games){ 
    isUserInteractionEnabled = false 
    inPlayLabel.isHidden = true // identifies that this game is in progress 
} 
// unset the sole game who's in progress 
if Int(fixture.currentGameTime) > 0 && !fixture.gameComplete { 
    isUserInteractionEnabled = true // makes this cell active 
    // show label as green dot 
    inPlayLabel.isHidden = false 
    inPlayLabel.layer.cornerRadius = 8 
    inPlayLabel.layer.masksToBounds = true 
    } 
} 

现在这个工程,当我有一个游戏进行中。我回到游戏列表中,并且可以访问活动的游戏并显示小标签和所有其他游戏单元不活动。但是如果我回去重置游戏,所以它不再活跃,所有的游戏都应该显示并且可以访问。但是活跃的游戏仍然显示为活跃游戏,所有其他游戏单元都无法访问。

我已经确认,当我通过在configureFixtureCell()中的isAnyGamesInProgress()调用中打印出消息来重置游戏时,它不会被归类为仍然活动的游戏。所以不确定为什么这些行似乎没有更新,特别是当我重新加载表时,游戏列表控制器willAppear()?而且我不缓存游戏NSFecthController :)

我已经附加了一些图片显示控制台输出过多的结果。首先是初始加载

Initial Load

比赛正在进行游戏复位enter image description here

+0

不能真正从代码告诉,但可以把它涉及到通过fetchedResultsController并获得在viewWillAppear中的最新数据不是一样像这样获得的副本'让games = fixture.team.game?.allObjects as! [游戏]' –

+0

@UpholderOfTruth我只是写一个for循环进FixtureViewCell打印出的价值观和他们相匹配的fetchedResultsController – Jonnny

+0

什么这行的值'如果INT(fixture.currentGameTime)> 0 &&!fixture.gameComplete { '?请问fixture.currentGameTime是否仍然大于零并且与fixture.team.game对象中的值不同? –

enter image description here

后看代码,它看起来像一个可重复使用的电池复位的问题, 。

尝试重置prepareForReuse()方法中的单元格。

覆盖它在你的子类,它都会被调用时,先前存在的细胞被dequeueReusableCellWithReuseIdentifier出队的时间。

实例斯威夫特

class myCell: UITableViewCell { 

    ... 
    ... 
    override func prepareForReuse() { 
    // your cleanup code 
    } 

} 
+1

我相信你是对的,因为我的解决方案是在'if isAnyGamesInProgress()'中明确设置两个条件。你能否为你的答案展示一个演示代码?我觉得你的答案可能更正确,从而对读者更有帮助。谢谢:-) – Jonnny

+0

用prepareForReuse()更新了答案 –