3 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
避免檢查iOS版本
id view = [tableViewCellInstance superview];
while (view && [view isKindOfClass:[UITableView class]] == NO) {
view = [view superview];
}
UITableView *tableView = (UITableView *)view;

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
在iOS7 beta 5中UITableViewWrapperView是的超級(jí)視圖UITableViewCell。也是UITableView一個(gè)的監(jiān)督UITableViewWrapperView。
因此,對(duì)于iOS 7,解決方案是
UITableView *tableView = (UITableView *)cell.superview.superview;
因此,對(duì)于iOS 6以下的iOS,解決方案是
UITableView *tableView = (UITableView *)cell.superview;

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
Swift 5擴(kuò)展
遞歸地
extension UIView {
func parentView<T: UIView>(of type: T.Type) -> T? {
guard let view = superview else {
return nil
}
return (view as? T) ?? view.parentView(of: T.self)
}
}
extension UITableViewCell {
var tableView: UITableView? {
return parentView(of: UITableView.self)
}
}
使用循環(huán)
extension UITableViewCell {
var tableView: UITableView? {
var view = superview
while let v = view, v.isKind(of: UITableView.self) == false {
view = v.superview
}
return view as? UITableView
}
}
- 3 回答
- 0 關(guān)注
- 1469 瀏覽
添加回答
舉報(bào)