滚动后 API 回调中的 Tableview 单元格数据已更改

Tableview cell data changed on API callback after scrolling

我的应用程序列出了一组提要。提要显示在 table 视图中。每个单元格都有一个 likebutton 和一个 feed 数据对象。单击赞按钮时,将发生 API 调用,它被写入 table 单元格子类中。 API 调用成功后,我需要更新 likebutton 图像和 feed 数据对象。但是如果我在启动 API 调用之后和接收 Onsuccess 之前滚动 table 视图,我在 Onsuccess 方法中引用的数据对象和 likebutton 将具有不同的索引(由于单元格重用)。如何在 API 调用开始时引用数据对象?我的代码如下。

#import "FeedCell.h"
- (IBAction)likeAction:(id)sender
{
 [APIManager unlikeORunlikePost:self.feedObject.entityID withSuccess:^(id response)
  {
  //Here I want to get the 'self.feedObject' which was passed to the API manager 
  //If I try to get 'self.feedObject' , that object will be different from what I passed initially if the tableview is scrolled before entering this success block
  } 
  andFailure:^(NSString *error)  
  {
 }];
}

弱捕获块中的提要对象并将其与当前提要对象的单元格进行比较,如果它们相同则单元格未被重用。

该块仅捕获 self,这是单元格,正如您所发现的,如果您滚动 table 并且单元格被重复使用,这会发生变化。

改为捕获特定数据对象:

- (IBAction)likeAction:(id)sender
{
 FeedObject *feedObject = self.feedObject;
 [APIManager unlikeORunlikePost:feedObject.entityID withSuccess:^(id response)
  {
      [feedObject doSomething]; // This will now be the original object
  //Here I want to get the 'self.feedObject' which was passed to the API manager 
  //If I try to get 'self.feedObject' , that object will be different from what I passed initially if the tableview is scrolled before entering this success block
  } 
  andFailure:^(NSString *error)  
  {
 }];
}

但是:

  • 在完成通话后,您的手机是否应该做更多事情?不就是另一个对象的责任吗?
  • 在完成块中返回受影响的对象会更整洁