【问题标题】:TableViewCell doesn't reload as expectedTableViewCell 未按预期重新加载
【发布时间】:2017-11-05 11:18:47
【问题描述】:

我有一个小应用程序可以跟踪玩家在体育比赛中的上场时间。我有一个体育游戏列表,您可以点击其中一个并开始跟踪游戏详情。我正在努力做到这一点,如果游戏正在进行并且用户返回游戏列表,他们就不能点击另一个游戏单元格,因为它会覆盖活动游戏中的所有当前数据。

我几乎完成了这项工作。当一场比赛正在进行时,我回到体育比赛列表,只有活动的比赛可以访问,并向用户显示它是活动的。但是当我返回并重置该游戏时,我希望所有体育游戏的 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 的结果 :)

我还附上了一些图片来显示控制台输出。首先是初始加载

游戏进行中

游戏重置后

【问题讨论】:

  • 无法从代码中真正看出,但它是否与通过 fetchedResultsController 在 vi​​ewWillAppear 中获取最新数据有关,并且与像这样获得的副本不一样 let games = fixture.team.game?.allObjects as! [Game]
  • @UpholderOfTruth 我刚刚在 FixtureViewCell 中写了一个 for 循环来打印出值,它们与 fetchedResultsController 的值相匹配
  • if Int(fixture.currentGameTime) > 0 && !fixture.gameComplete { 这一行怎么样?会不会是 fixture.currentGameTime 仍然高于零并且与 fixture.team.game 对象中的不同?
  • @Jonnny 你能分享一下你申请重置的逻辑吗?以便我可以进一步帮助您。

标签: ios swift uitableview tableviewcell prepareforreuse


【解决方案1】:

看代码,好像是可重用的单元格重置问题。

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

在您的单元格子类中覆盖它,每次当先前存在的单元格被 dequeueReusableCellWithReuseIdentifier 出列时,都会调用它。

Swift 中的示例

class myCell: UITableViewCell {

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

}

【讨论】:

  • 我相信你是对的,因为我的解决方案是在if isAnyGamesInProgress() 中明确设置这两个条件。您能否显示演示代码以获取答案。我觉得你的回答可能更正确,因此对读者更有帮助。谢谢:-)
猜你喜欢
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多