关于UIView的setNeedsDisplay的困惑

时间:2011-08-26 12:07:09

标签: iphone objective-c ios uitableview setneedsdisplay

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

我正在寻找关于setNeedsDisplay的解释?在UITableView实例上调用时。

根据文档setNeedsDisplay在“下一个绘图周期”中调用drawRect。所以任何人都可以告诉我下一个绘图周期是什么。

5 个答案:

答案 0 :(得分:6)

setNeedsDisplay 方法与重新加载 tableView 无关。它只是重绘 tableView view,而不是单元格分隔符

setNeedsDisplay reloadData 完全用于不同目的。

答案 1 :(得分:5)

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

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

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

答案 2 :(得分:2)

SetNeedsDisplay

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

<强> reloadData

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

我的解决方案是更新所有可见单元格并跟踪我的自定义类中的单元格的indexPath&#34; ProductTableViewCell&#34;使用reloadRowsAtIndexPaths,见下文:

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;
相关问题