我有一个UITableViewCell
,它链接到一个对象,我需要判断该单元格是否可见.从我所做的研究来看,这意味着我需要以某种方式访问包含它的UITableView
(从那里,有几种方法可以判断它是否可见).所以我想知道UITableViewCell
是否有指向UITableView
的指针,或者有没有其他方法可以从单元格中获取指针?
我有一个UITableViewCell
,它链接到一个对象,我需要判断该单元格是否可见.从我所做的研究来看,这意味着我需要以某种方式访问包含它的UITableView
(从那里,有几种方法可以判断它是否可见).所以我想知道UITableViewCell
是否有指向UITableView
的指针,或者有没有其他方法可以从单元格中获取指针?
为避免判断IOS版本,请从单元视图反复向上查看超级视图,直到找到UITableView:
Objective-C
id view = [cellInstance superview];
while (view && [view isKindOfClass:[UITableView class]] == NO) {
view = [view superview];
}
UITableView *tableView = (UITableView *)view;
Swift
var view = cellInstance.superview
while (view != nil && (view as? UITableView) == nil) {
view = view?.superview
}
if let tableView = view as? UITableView {
tableView.beginUpdates()
tableView.endUpdates()
}