gpt4 book ai didi

ios - 在 cellForRowAtIndexPath 中执行繁重的计算

转载 作者:行者123 更新时间:2023-11-29 02:39:46 32 4
gpt4 key购买 nike

我有一个加载产品的表格 View 。每个的价格都需要一些繁重的计算,所以我想在主 UI 线程之外进行此操作,因为否则滚动表格会非常非常跳跃,几乎不可能。无论我尝试什么,我都无法正确理解语法和概念。我当前的代码确实在后台加载了价格,但是当我滚动时,一些单元格给出了不属于它们的价格。我不知道如何调试这个。任何建议表示赞赏。

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.products count] == 0) {
UITableViewCell *cell = [[UITableViewCell alloc] init];
return cell;
}

AvailableCustomerProductTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;

cell.buttonAdd.tag = indexPath.row;
[cell.buttonAdd addTarget: self action: @selector(addToSelectedProduct:) forControlEvents: UIControlEventTouchUpInside];

Product *prod = nil;
if (theTableView == self.searchDisplayController.searchResultsTableView) {
prod = (Product *)[self.filteredProducts objectAtIndex:indexPath.row];
} else {
prod = (Product *)[self.products objectAtIndex:indexPath.row];
}

if (prod != nil) {
cell.pNumber.text = prod.number;
cell.description.text = prod.desc;

if ([Common getProductPromotion:prod] != nil) {
cell.btnPromotionTag.hidden = NO;
cell.btnPromotionTag.tag = indexPath.row;
[cell.btnPromotionTag addTarget: self action: @selector(showPromotionDetails:) forControlEvents: UIControlEventTouchUpInside];
}
else{
cell.btnPromotionTag.hidden = YES;
}

//Get the customer product price, first:
//If if the product has a record in the productCustomerPrices list
//if not get the price from the standard price.
if (self.order.orderOrderCustomer != nil) {
// Do the Customer price fetching in a background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CustomerPrice *custPrice = [Common getPriceForCustomer:self.order.orderOrderCustomer.customerId forProduct:prod.productId inManagedObjectContext:self.backgroundContext];

// Return to the main thread to update the UI
dispatch_async(dispatch_get_main_queue(), ^{
if (custPrice) {
NSManagedObjectID *objId = custPrice.objectID;
CustomerPrice *custPrice = (CustomerPrice*)[self.managedObjectContext objectWithID:objId];

if (custPrice) {
// Check that the relevant data is still required and get the correct cell (it might have changed)
AvailableCustomerProductTableViewCell * correctCell = nil;
if ([[theTableView indexPathsForVisibleRows] containsObject:indexPath]) {
correctCell = (AvailableCustomerProductTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
}

[correctCell.btnPrice setTitle:[Common getCurrencyFormattedStringFromFloat:[custPrice.price floatValue]] forState:UIControlStateNormal];
[correctCell.btnPrice setTitleColor:[UIColor colorWithRed:0.01 green:0.65 blue:0.77 alpha:1] forState:UIControlStateNormal];
correctCell.btnPrice.enabled = NO;
[correctCell setNeedsLayout];
} }
});
});
}
}

UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)];
[sgr setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:sgr];

return cell;
}

最佳答案

您是否尝试过分几个步骤执行此操作:

  1. 在加载 View 时(在 viewWillAppear: 中)加载您的产品列表以快速显示 ListView,
  2. 在后台调用服务器以在单独的 NSDictionary*prices 中检索价格列表,键为 productId。当计算结束后,你应该在主线程调用[self.tableView reloadData]!
  3. cellForRowAtIndexPath: 方法中,您应该只检查您的 prices 字典中是否有价格并显示您的按钮。如果您没有价格,请务必隐藏该按钮,以避免在回收电池时出现显示故障。

像这样,您可以避免一些复杂的按行和关联重新加载,这可能会导致您的问题。

关于ios - 在 cellForRowAtIndexPath 中执行繁重的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25933915/

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