Ios 如何在UITableVIewCell中进行API调用
我有一个场景,在这个场景中,我必须发出API请求来更新Ios 如何在UITableVIewCell中进行API调用,ios,objective-c,uitableview,afnetworking,tableviewcell,Ios,Objective C,Uitableview,Afnetworking,Tableviewcell,我有一个场景,在这个场景中,我必须发出API请求来更新TableViewCell中的UILables 问题是,对于每个单元格,我必须发出唯一的API请求。APIurl相同,但参数不同 目前,我正在cellforrowatinex中调用,在success block中,我正在使用dispatch\u async更新数组并重新加载UITableView MyCellForRowatineXmethod: if(!apiResponded) //Bool value to check API has
TableViewCell
中的UILables
问题是,对于每个单元格,我必须发出唯一的API
请求。API
url相同,但参数不同
目前,我正在cellforrowatinex
中调用,在success block中,我正在使用dispatch\u async
更新数组并重新加载UITableView
MyCellForRowatineXmethod
:
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];
});
});
当我运行此代码时,每个标签的值都相同。我不知道我的方法是好是坏。请任何人建议如何实现这一点。您可以在自定义单元格中声明一个方法,然后从CellForRowatinex调用它,该方法将调用API并更新仅存在于该单元格中的标签。
因此,对于每个单元格,您将有单独的方法调用&每个成功块将只更新特定的单元格标签文本 从您的代码中,我不确定您想要实现什么。我所知道的是,您希望对每个单元格发出请求,并显示收到的数据。现在我不知道您希望如何存储数据,或者如何设置数据,但我会给您一个简单的建议,说明如何设置数据,然后您可以根据需要进行修改 我想您只需要在每个单元格中发出一次请求。为了简单起见,我们可以为接收到的数据(作者姓名?)存储一个字典 我们需要在使用之前,在init或ViewDidLoad内部,或者在您认为合适的任何地方(只要是在TableView调用CellForRowatineXpath:)实例化它 现在,在cellForRowAtIndexPath中,您可以执行以下操作:
NSInteger index = indexPath.row
cell.authorLabel.text = nil;
cell.tag = index
NSString *authorName = authorNames[@(index)];
if (authorName) { // Check if name has already exists
cell.authorLabel.text = authorName;
} else {
// Make request here
}
在请求完成块(CellForRowatineXpath:内)中,添加以下内容:
NSString *authorName = [responseData valueForKey:@“aName”];
authorNames[@(index)] = authorName; // Set the name for that index
if (cell.index == index) { // If the cell is still being used for the same index
cell.authorLabel.text = authorName;
}
当您在TableView中上下滚动时,它将重用屏幕外滚动的单元格。这意味着当一个请求完成时,单元格可能会被滚动到屏幕外,并被重新用于另一个索引。因此,您需要设置单元格标记,当请求完成后,检查该单元格是否仍在用于您请求的索引
潜在问题:当快速上下滚动时,当您的请求仍在加载时,可能会对每个单元格发出多个请求。您必须添加一些方法,使每个请求只发出一次 thanx@mbo42我遵循了您的步骤,在更改了一些步骤后,我终于实现了:)很高兴为您服务:)
NSInteger index = indexPath.row
cell.authorLabel.text = nil;
cell.tag = index
NSString *authorName = authorNames[@(index)];
if (authorName) { // Check if name has already exists
cell.authorLabel.text = authorName;
} else {
// Make request here
}
NSString *authorName = [responseData valueForKey:@“aName”];
authorNames[@(index)] = authorName; // Set the name for that index
if (cell.index == index) { // If the cell is still being used for the same index
cell.authorLabel.text = authorName;
}