gpt4 book ai didi

ios - 是什么触发 UITableView 第一次加载它的数据?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:02:42 26 4
gpt4 key购买 nike

当您使用委托(delegate)和数据源创建 TableView 时,您可以对其调用 reloadData 以强制它转到数据源并获取数据并显示它。

但是,您不需要第一次就这样做。首先转到其数据源并开始加载数据的钩子(Hook)是什么?

UIView 上没有 viewDidAppear 或类似的东西。它不能初始化,因为它还没有数据源。

我正在尝试创建我自己的以类似方式工作的控件,并且我正在尝试找到一种可用于触发对数据源的首次调用的方法。

最佳答案

我认为对于您的自定义表类,最佳解决方案是:1) 在类中有状态 isReloading

- (void)reloadData {
_reloading = YES;
// Do someting
_reloading = NO;
}

2) 在 willMoveToSuperview 检查你有数据源并且没有重新加载:

- (void)willMoveToSuperview:(UIView *)superview {
if (self.dataSource && !self.isReloading) {
[self reloadData];
}
[super willMoveToSuperview:superview];
}

3) 在 setDataSource 上检查是否有 superview 并且不重新加载:

- (void)setDataSource:(id<YourProtocolDelegate>)dataSource {
_dataSource = dataSource;
if (self.superview && !self.isReloading) {
[self reloadData];
}
}

有关 UITableView 函数调用的信息:

对于 Storyboard加载:

2014-12-19 02:46:42.264 TestObjectiveC[21574:1891199] initWithCoder:
2014-12-19 02:46:42.266 TestObjectiveC[21574:1891199] setNeedsLayout
2014-12-19 02:46:42.266 TestObjectiveC[21574:1891199] setNeedsDisplay
2014-12-19 02:46:42.269 TestObjectiveC[21574:1891199] awakeAfterUsingCoder:
2014-12-19 02:46:42.269 TestObjectiveC[21574:1891199] setDataSource:
2014-12-19 02:46:42.270 TestObjectiveC[21574:1891199] setDelegate:
2014-12-19 02:46:42.270 TestObjectiveC[21574:1891199] awakeFromNib
2014-12-19 02:46:42.271 TestObjectiveC[21574:1891199] setNeedsDisplay
2014-12-19 02:46:42.298 TestObjectiveC[21574:1891199] willMoveToSuperview:
2014-12-19 02:46:42.300 TestObjectiveC[21574:1891199] didMoveToSuperview
2014-12-19 02:46:42.304 TestObjectiveC[21574:1891199] willMoveToWindow:
2014-12-19 02:46:42.306 TestObjectiveC[21574:1891199] didMoveToWindow
2014-12-19 02:46:42.307 TestObjectiveC[21574:1891199] setNeedsLayout
2014-12-19 02:46:42.342 TestObjectiveC[21574:1891199] setNeedsLayout
2014-12-19 02:46:42.343 TestObjectiveC[21574:1891199] layoutSubviews
2014-12-19 02:46:42.344 TestObjectiveC[21574:1891199] setNeedsLayout
2014-12-19 02:46:42.345 TestObjectiveC[21574:1891199] reloadData
2014-12-19 02:46:42.348 TestObjectiveC[21574:1891199] layoutSubviews

回溯:

  * frame #0: 0x000621bc TestObjectiveC`-[CustomTableView reloadData](self=0x7a37c400, _cmd=0x01915284) + 28 at CustomTableView.m:15
frame #1: 0x0112232e UIKit`-[UITableView _reloadDataIfNeeded] + 78
frame #2: 0x01128317 UIKit`-[UITableView layoutSubviews] + 36
frame #3: 0x000623d8 TestObjectiveC`-[CustomTableView layoutSubviews](self=0x7a37c400, _cmd=0x01915520) + 120 at CustomTableView.m:34
frame #4: 0x0109ddd1 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 608
frame #5: 0x0055d771 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
frame #6: 0x0463d28f QuartzCore`-[CALayer layoutSublayers] + 152
frame #7: 0x04631115 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 397
frame #8: 0x04630f70 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 26
frame #9: 0x0458f3c6 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 284
frame #10: 0x0459078c QuartzCore`CA::Transaction::commit() + 392
frame #11: 0x04656799 QuartzCore`+[CATransaction flush] + 52
frame #12: 0x01010286 UIKit`-[UIApplication _reportMainSceneUpdateFinished:] + 39
frame #13: 0x01011201 UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 3163

对于 initWithFrame 加载:

2014-12-19 02:47:55.601 TestObjectiveC[21806:1895258] initWithFrame:style:
2014-12-19 02:47:55.604 TestObjectiveC[21806:1895258] setNeedsLayout
2014-12-19 02:47:55.605 TestObjectiveC[21806:1895258] setNeedsDisplay
2014-12-19 02:47:55.606 TestObjectiveC[21806:1895258] setDelegate:
2014-12-19 02:47:55.607 TestObjectiveC[21806:1895258] setDataSource:
2014-12-19 02:47:55.608 TestObjectiveC[21806:1895258] willMoveToSuperview:
2014-12-19 02:47:55.610 TestObjectiveC[21806:1895258] didMoveToSuperview
2014-12-19 02:47:55.640 TestObjectiveC[21806:1895258] willMoveToWindow:
2014-12-19 02:47:55.641 TestObjectiveC[21806:1895258] didMoveToWindow
2014-12-19 02:47:55.642 TestObjectiveC[21806:1895258] setNeedsLayout
2014-12-19 02:47:55.680 TestObjectiveC[21806:1895258] setNeedsLayout
2014-12-19 02:47:55.683 TestObjectiveC[21806:1895258] layoutSubviews
2014-12-19 02:47:55.684 TestObjectiveC[21806:1895258] setNeedsLayout
2014-12-19 02:47:55.684 TestObjectiveC[21806:1895258] reloadData
2014-12-19 02:47:55.686 TestObjectiveC[21806:1895258] layoutSubviews

回溯:

* frame #0: 0x0008517c TestObjectiveC`-[CustomTableView reloadData](self=0x7ba7f400, _cmd=0x01939284) + 28 at CustomTableView.m:15
frame #1: 0x0114632e UIKit`-[UITableView _reloadDataIfNeeded] + 78
frame #2: 0x0114c317 UIKit`-[UITableView layoutSubviews] + 36
frame #3: 0x00085398 TestObjectiveC`-[CustomTableView layoutSubviews](self=0x7ba7f400, _cmd=0x01939520) + 120 at CustomTableView.m:34
frame #4: 0x010c1dd1 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 608
frame #5: 0x00581771 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
frame #6: 0x0466128f QuartzCore`-[CALayer layoutSublayers] + 152
frame #7: 0x04655115 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 397
frame #8: 0x04654f70 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 26
frame #9: 0x045b33c6 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 284
frame #10: 0x045b478c QuartzCore`CA::Transaction::commit() + 392
frame #11: 0x0467a799 QuartzCore`+[CATransaction flush] + 52
frame #12: 0x01034286 UIKit`-[UIApplication _reportMainSceneUpdateFinished:] + 39
frame #13: 0x01035201 UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 3163
frame #14: 0x0104d7d0 UIKit`__84-[UIApplication

更新:更正确的代码

1) 在类中有状态 isReloading

- (void)reloadData {
// More correct code, due that user can trigger reloadData multiple times, and you can reload it async. So in this case it never calls few times at the same time
if (self.isReloading) return;
_reloading = YES;
// Do someting
_reloading = NO;
}

2) 在 willMoveToSuperview 检查你有数据源并且没有重新加载:

- (void)willMoveToSuperview:(UIView *)superview {
if (self.dataSource) {
[self reloadData];
}
[super willMoveToSuperview:superview];
}

3) 在 setDataSource 上检查是否有 superview 并且不重新加载:

- (void)setDataSource:(id<YourProtocolDelegate>)dataSource {
_dataSource = dataSource;
if (self.superview) {
[self reloadData];
}
}

关于ios - 是什么触发 UITableView 第一次加载它的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27558047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com