使用NSOperationQueue加载UITableViewCell

时间:2022-10-11 17:36:02

Hi guys I have an UITableView that loads precomputed cells from NSMutableArray. I want to use NSOperationQueue or PerformSelectorOnMainThread to update the user interface to enable smooth scrolling but i get an error... this is my code...

大家好,我有一个UITableView,从NSMutableArray加载预先计算的单元格。我想使用NSOperationQueue或PerformSelectorOnMainThread更新用户界面以启用平滑滚动但我收到错误...这是我的代码...

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //queue is being initialized in viewDidLoad          
  [queue addOperationWithBlock:^ {
      [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
          NSLog(@"Updating...");      
          return [self.CellObjects objectAtIndex:indexPath.row];          
          //if you remove the line above with the return, NSOperationQueue will work but I need the above line to load the cell.
      }];
  }];
}

Is there a way to make it work? Any help appreciated!

有没有办法让它发挥作用?任何帮助赞赏!

2 个解决方案

#1


3  

Why not simple ...

为什么不简单......

return [self.CellObjects objectAtIndex:indexPath.row];

...?

It's a mess. Why do you have two calls to addOperationWithBlock:? And also your return statement has nothing to do with return value of tableView:cellForRowAtIndexPath:. It's return value of your block, so, it will never work.

一团糟。为什么有两个addOperationWithBlock调用:?并且你的return语句与tableView的返回值无关:cellForRowAtIndexPath:。这是你的块的返回值,因此,它永远不会工作。

What's your error? I assume it's about incompatible block pointer, because it expects void(^)(void) and you're trying to send UITableViewCell *(^)(void).

你的错误是什么?我假设它是关于不兼容的块指针,因为它期望void(^)(void)并且您尝试发送UITableViewCell *(^)(void)。

Blocks are not gonna help you there. If you have precomputed cells in CellObjects, just use only the return self.CellObjects[indexPath.row]; line.

街区不会帮助你。如果您在CellObjects中预先计算了单元格,则只使用return self.CellObjects [indexPath.row];线。

Also don't use property names like CellObjects. Should be named cellObjects. Check the case.

也不要使用像CellObjects这样的属性名称。应该命名为cellObjects。检查案例。

#2


0  

Offsetting your cell retrieval in that way is not going to give you any advantage. You need to compute your cell height/size/content prior to your UITableView instance asking for cells.

以这种方式抵消细胞检索不会给你任何好处。您需要在UITableView实例请求单元格之前计算单元格高度/大小/内容。

UITableView is expecting a UITableViewCell to be returned from that delegate callback on the main thread.

UITableView期望从主线程上的该委托回调返回UITableViewCell。

A better idea would be to place the computation on other threads if they require time and their on completion you can call back to your UITableView to reloadData.

更好的想法是将计算放在其他线程上,如果它们需要时间并且在完成时你可以回调你的UITableView来重新加载数据。

#1


3  

Why not simple ...

为什么不简单......

return [self.CellObjects objectAtIndex:indexPath.row];

...?

It's a mess. Why do you have two calls to addOperationWithBlock:? And also your return statement has nothing to do with return value of tableView:cellForRowAtIndexPath:. It's return value of your block, so, it will never work.

一团糟。为什么有两个addOperationWithBlock调用:?并且你的return语句与tableView的返回值无关:cellForRowAtIndexPath:。这是你的块的返回值,因此,它永远不会工作。

What's your error? I assume it's about incompatible block pointer, because it expects void(^)(void) and you're trying to send UITableViewCell *(^)(void).

你的错误是什么?我假设它是关于不兼容的块指针,因为它期望void(^)(void)并且您尝试发送UITableViewCell *(^)(void)。

Blocks are not gonna help you there. If you have precomputed cells in CellObjects, just use only the return self.CellObjects[indexPath.row]; line.

街区不会帮助你。如果您在CellObjects中预先计算了单元格,则只使用return self.CellObjects [indexPath.row];线。

Also don't use property names like CellObjects. Should be named cellObjects. Check the case.

也不要使用像CellObjects这样的属性名称。应该命名为cellObjects。检查案例。

#2


0  

Offsetting your cell retrieval in that way is not going to give you any advantage. You need to compute your cell height/size/content prior to your UITableView instance asking for cells.

以这种方式抵消细胞检索不会给你任何好处。您需要在UITableView实例请求单元格之前计算单元格高度/大小/内容。

UITableView is expecting a UITableViewCell to be returned from that delegate callback on the main thread.

UITableView期望从主线程上的该委托回调返回UITableViewCell。

A better idea would be to place the computation on other threads if they require time and their on completion you can call back to your UITableView to reloadData.

更好的想法是将计算放在其他线程上,如果它们需要时间并且在完成时你可以回调你的UITableView来重新加载数据。