标签: prepareforreuse

使用prepareForReuse的正确方法是什么?

需要帮助了解如何在UIKit中使用prepareForReuse().该文件说:

您应该只重置与内容无关的单元格属性,例如,alpha,编辑和选择状态

但重置个别属性属性如isHidden呢?

假设我的单元格有2个标签,我应该重置:

  1. label.text
  2. label.numberOfLines
  3. label.isHidden

我的tableView(_:cellForRowAt :)委托有条件逻辑来隐藏/显示每个单元格的标签.

uitableview ios swift prepareforreuse

7
推荐指数
1
解决办法
8437
查看次数

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
 } …
Run Code Online (Sandbox Code Playgroud)

uitableview tableviewcell ios swift prepareforreuse

7
推荐指数
1
解决办法
174
查看次数

如何在重新启动后重新绘制不可见的UICollectionViewCell?

如何在重用发生时重绘不可见的UICollectionViewCell?

我想到的一种方法是使用Layout Cell prepareForReuse函数中的代码,但是它的工作方式非理想,因为它会导致需要更多的重新绘制.

背景:需要drawRect在方向更改后触发单元格,这些方向更改当前不可见,但弹出使用并且尚未重绘,所以到目前为止我只能看到这prepareForReuse是合适的.问题是我正在重新绘制所有"重用"单元格,而我真的只想重绘最初弹出的那些在设备的前一个方向位置创建的单元格.

附加信息:所以目前我这样做:

在ViewController中:

override func viewWillLayoutSubviews() {
    // Clear cached layout attributes (to ensure new positions are calculated)
    (self.cal.collectionViewLayout as! GCCalendarLayout).resetCache()
    self.cal.collectionViewLayout.invalidateLayout()

    // Trigger cells to redraw themselves (to get new widths etc)
    for cell in self.cal?.visibleCells() as! [GCCalendarCell] {
        cell.setNeedsDisplay()
    }

    // Not sure how to "setNeedsDisplay" on non visible cells here?
}
Run Code Online (Sandbox Code Playgroud)

在Layout Cell类中:

override func prepareForReuse() {
    super.prepareForReuse()
    // Ensure "drawRect" is called (only way I could see to …
Run Code Online (Sandbox Code Playgroud)

ios uicollectionview uicollectionviewcell swift prepareforreuse

5
推荐指数
1
解决办法
2329
查看次数

UITableViewCell 中不遵守 NSLayoutConstraints

我有一个UITableViewCellUILabelUIImageView。图像可以是可见的或隐藏的。

这是我的故事板: 故事板截图

的尾随部分有两个约束UILabel,一个 (a) 等于 8 与UIImageView,另一个 (b) 大于或等于 8 与单元格的右边距。我保留第一个 (a) 的引用,如果有声音或没有声音,我会激活或停用约束。

这是我的代码:

class MyTableViewCell: UITableViewCell {

    @IBOutlet weak var label: UILabel?
    @IBOutlet weak var icon: UIImageView?
    @IBOutlet weak var spaceBetweenIconAndLabelConstraint: NSLayoutConstraint?

    override func awakeFromNib() {
        super.awakeFromNib()
        icon?.image = UIImage(named: "sound")
    }

    func config(with name: String, hasSound: Bool) {
        label?.text = name
        configSound(hasSound)
    }

    private func configSound(_ hasSound: Bool) {
        icon?.isHidden = !hasSound
        spaceBetweenIconAndLabelConstraint?.isActive = hasSound
    }
}
Run Code Online (Sandbox Code Playgroud)

我有几个带有声音图标的单元格,很多没有。这是特定单元格首次出现时的样子: …

uitableview ios nslayoutconstraint swift prepareforreuse

3
推荐指数
1
解决办法
464
查看次数