【发布时间】:2023-04-03 03:05:02
【问题描述】:
我有一个场景,我必须发出 API 请求来更新 UILables 中的 TableViewCell 。
问题在于,对于每个单元格,我必须发出唯一的API 请求。 API url 相同,但参数不同。
目前我正在调用cellForRowAtIndex,在成功块中我使用dispatch_async 更新数组并重新加载UITableView。
我的cellForRowAtIndexMethod:
if(!apiResponded) //Bool value to check API hasn't responded I have to make API request
{
cell.authorLabel.text = @"-------";// Set Nil
NSString *userId =[CacheHandler getUserId];
[self.handleAPI getAuthorList:userId]; //make API Call
}
else
{
cell.authorLabel.text = [authorArray objectAtIndex:indexPath.row];// authorArray is global Array
}
我的 API 请求成功块:
numOfCallsMade = numOfCallsMade+1; //To track how manny calls made
apiResponded = YES; // to check API is reponded and I have to update the UILables
dispatch_async(kBgQueue, ^{
if(!authorArray)
authorArray = [[NSMutableArray alloc]init];
NSArray *obj = [responseData valueForKey:@"aName"];
if(obj == nil)
{
[authorArray addObject:@"N/A"];
}
else
{
[authorArray addObject:[obj valueForKey:@"authorName"]];
}
dispatch_async(dispatch_get_main_queue(), ^{
if(numOfCallsMade == [self.mCarsArray count]) // this is to check if I have 10 rows the 10 API request is made then only update
[self.mTableView reloadData];
});
});
当我运行此代码时,我会为每个标签获得相同的值。我不知道我的方法好不好。请任何人建议如何实现这一目标。
【问题讨论】:
标签: ios objective-c uitableview afnetworking tableviewcell