简体   繁体   English

什么时候是tableView:numberOfRowsInSection:在UITableView中调用?

[英]When is tableView:numberOfRowsInSection: called in UITableView?

tableView:numberOfRowsInSection is sent to the delegate of a UITableView to find out how many rows it needs to have in a given section. tableView:numberOfRowsInSection被发送到UITableView的委托,以找出它在给定部分中需要有多少行。

My question is, when and how often is this method called? 我的问题是,这个方法的调用时间和频率是多少?

在第一次加载tableview时调用该方法,如果您对委托更感兴趣,则放置一个断点并检查调用哪个委托的时间和位置以及多少次。

Below are the instances when that function will get called, 下面是调用该函数的实例,

  1. For the first time when table is loaded 第一次加载表时
  2. the time you reload the table data 重新加载表数据的时间
  3. the time you add/update/delete your row or sections dynamically. 您动态添加/更新/删除行或部分的时间。

The method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section is a protocol method of the UITableViewDataSource - protocol . 方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionUITableViewDataSource - 协议的协议方法。 It will be called the very first time your table view is loaded based on that you have set the dataSource properly, eg 它将在您第一次加载表视图时调用,具体取决于您已正确设置dataSource ,例如

self.yourTableView.dataSource = self;

If you are interested in updating your table again at a later time you can call 如果您有兴趣稍后再次更新您的桌子,可以致电

[self.yourTableView reloadData];

in order to reload the entire table. 为了重新加载整个表。 If you are only interested in reloading a part of your table you can do something similar to 如果您只对重新加载表的一部分感兴趣,可以执行类似的操作

NSIndexSet *reloadSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSectionsInTableView:self.yourTableView])];
[self.yourTableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic];

Hope it helps! 希望能帮助到你!

My question is, when and how often is this method called? 我的问题是,这个方法的调用时间和频率是多少?

Short Answer : When your UITableView needs to update something. 简答:当您的UITableView需要更新某些内容时。

Long Answer : Delegates Methods generally called themselves however it may be called multiple times when your UITableView needs to update something. 长答案:代表方法通常称自己,但是当您的UITableView需要更新某些内容时,它可能被多次调用。 By default, it's called very first time the tableview is getting loaded or updated (reloaded). 默认情况下,第一次调用tableview时会调用它(重新加载)。

It depends on how often user will scroll UITable view to section and how many sections there are. 这取决于用户将UITable视图滚动到部分的频率以及有多少部分。 This value, which is returned by this function and is casched. 此值,由此函数返回并进行转换。 Method will need be revoked if you will update content of table view (filtering results, or updating data via reloadData ). 如果要更新表视图的内容(过滤结果或通过reloadData更新数据),则需要撤销方法。

Best thing for you will be to add logging to this function and check this yourself. 最好的办法是将记录添加到此功能并自行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用UITableView时,ios-tableView:numberOfRowsInSection…NSInvalidArgumentException - ios - tableView:numberOfRowsInSection …NSInvalidArgumentException when working with UITableView tableView:numberOfRowsInSection:连续调用两次 - tableView:numberOfRowsInSection: called twice in a row 搜索时发生tableView:numberOfRowsInSection错误 - tableView:numberOfRowsInSection error when searching 在初始加载时,三次调用UITableView numberOfSectionsInTableView和numberOfRowsInSection - UITableView numberOfSectionsInTableView and numberOfRowsInSection called thrice on initial load 为什么UITableView numberOfRowsInSection被多次调用? - Why UITableView numberOfRowsInSection is called many times? UITableView numberOfRowsInSection - UITableView numberOfRowsInSection 什么时候被称为:-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - when is this called : - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath UITableView,numberOfRowsInSection - UITableView,numberOfRowsInSection 在tableView(_:numberOfRowsInSection :)中引发错误时该怎么办? - What to do when error thrown in tableView(_:numberOfRowsInSection:)? 重新加载 tableView 时将 numberOfRowsInSection 发送到 UIProxyObject - numberOfRowsInSection being sent to UIProxyObject when reloading tableView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM