gpt4 book ai didi

ios - 正确设置 numberOfRowsInSection

转载 作者:行者123 更新时间:2023-11-29 02:15:30 25 4
gpt4 key购买 nike

我有一个使用 AFNetworking 加载数据的应用程序,解析收入 JSON 数据并填充表。我使用不同的类来管理 JSON 并填充 UITableView

我需要正确设置行数。问题是,我猜它的方法加载得太早了,在我们从网络上获取任何数据之前。我试过以下:

-(NSInteger)numberOfElements{


return [self.dataDictArray count];

然后:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [self.handler numberOfElements];
}

当处理程序是类的实例类型时,处理 JSON 并声明 -(NSInteger)numberOfElements

在这种情况下如何设置正确的元素数量?显然 tableView 在我们获取网络数据之前 加载,这有点令人困惑。

最佳答案

一种方法是:

在 AFNetworking get 方法的成功 block 中触发通知

  #define NOTIFICATION_NAME @"LoadNotification"  

[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_NAME object:self];

在UITableViewController子类的viewDidLoad方法中将该类设置为通知的观察者

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedLoadingNotification:) name:NOTIFICATION_NAME object:nil];

在“receivedLoadingNotification”方法中设置委托(delegate)和datasourceDelegate

-(void)receivedLoadingNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:NOTIFICATION_NAME])
{
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
}
}

因此,只有当 JSON 数据从 AFNetworking 成功加载时, Controller 才会调用 dataSource 和 delegate 方法。希望对您有所帮助。

关于ios - 正确设置 numberOfRowsInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28796920/

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