Iphone 关于UIView的困惑';s设置需要显示

Iphone 关于UIView的困惑';s设置需要显示,iphone,objective-c,ios,uitableview,setneedsdisplay,Iphone,Objective C,Ios,Uitableview,Setneedsdisplay,我有一个UITableView*myTableView的ivar。我有从internet加载的数据,所以当数据加载时,我应该调用[myTableView setNeedsDisplay],我知道我可以/应该调用[myTableView reloadData],但混淆的是调用setNeedsDisplay也应该工作,但不工作 我只是想了解一下setNeedsDisplay的功能?在UITableView实例上调用时 根据文档集NeedsDisplay在“下一个绘图周期”中调用drawRect。那么

我有一个UITableView*myTableView的ivar。我有从internet加载的数据,所以当数据加载时,我应该调用[myTableView setNeedsDisplay],我知道我可以/应该调用[myTableView reloadData],但混淆的是调用setNeedsDisplay也应该工作,但不工作

我只是想了解一下setNeedsDisplay的功能?在UITableView实例上调用时

根据文档集NeedsDisplay在“下一个绘图周期”中调用drawRect。那么谁能告诉我下一个绘图周期是什么呢。

设置需要显示方法与重新加载tableView无关。它只是重新绘制tableView的
视图
,而不是其单元格或分隔符


setNeedsDisplay和reloadData的用途完全不同。

setNeedsDisplay
将调用的UIView标记为需要重新绘制纯粹是一种视觉感觉,如前所述,这将在下一个绘制周期发生

这与在
UITableView
上下文中使用时重新加载数据完全不同


有关此主题的更多信息,请参阅。

通常,您只需要在已实现drawRect的自定义视图上调用setNeedsDisplay。

setNeedsDisplay

这方面的一个很好的例子是,使用一个图像渲染UIImageView;当它仍在屏幕上时,您可以将图像更改为其他内容。在这种情况下,仅更改图像并不总是会触发视图的重新绘制,为此,您需要调用
setNeedsDisplay

重新加载数据


当tableview的基础数据源发生更改并且您希望在UI中重新选择它时,必须调用它。在这种情况下,调用此
reloadData
mehod

我的解决方案是更新所有可见单元格,并使用reloadrowsatindexpath跟踪自定义类“ProductTableViewCell”中单元格的indexPath,请参见下文:

NSArray *arrayCells = [self.tableView visibleCells];
NSMutableArray *mArrayIndexPaths = [[NSMutableArray alloc] init];
for(ProductTableViewCell *cell in arrayCells) {
    [mArrayIndexPaths addObject:cell.indexPath];
}

[self.tableView beginUpdates];
[self.tableView endUpdates];
[self.tableView reloadRowsAtIndexPaths:[mArrayIndexPaths copy] withRowAnimation:UITableViewRowAnimationAutomatic];
下面是类的定义方式:

@interface ProductTableViewCell : UITableViewCell {
    NSString *reuseID;
}

@property (nonatomic, strong) NSIndexPath *indexPath;

链接未转到相关内容