检测 UITableViewCell 何时加载 [英] Detect when UITableViewCell did load
问题描述
我需要能够检测到我的自定义 UITableViewCell
何时加载到屏幕上,以便我可以以编程方式对其中的视图执行操作.我可以使用的唯一代码是在自定义 UITableViewCell
中实现 layoutSubviews()
,但这是不好的做法,因为 layoutSubviews()
被称为更多不止一次.此外,将我的代码放在 awakeFromNib()
中不起作用,因为我需要调用当时尚未设置的委托属性.我怎样才能做到这一点?
I need to be able to detect when my custom UITableViewCell
has been loaded on screen, so that I can programmatically do stuff to the view inside it. The only code I could get working was implementing layoutSubviews()
inside the custom UITableViewCell
, but this is bad practice because layoutSubviews()
is called more than one time. Also, putting my code in awakeFromNib()
doesn't work because I need to call a delegate property, which hasn't been set at that point. How can I achieve this?
推荐答案
有两种不同的方法可以做到.
There are two different ways you could do it.
在您的 UITableViewDelegate
中,您可以实现在单元格即将显示时调用的委托方法.
In your UITableViewDelegate
you can implement the delegate method that is called when a cell is about to be displayed.
func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// Update the cell
}
或者在你的 UITableViewCell
子类中你可以实现 didMoveToSuperView
.
Or in your UITableViewCell
subclass you could implement didMoveToSuperView
.
override func didMoveToSuperview() {
super.didMoveToSuperview()
if superview != nil {
// Update the cell
}
}
这篇关于检测 UITableViewCell 何时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!