3

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

我只是在寻找关于 setNeedsDisplay 做什么的解释?在 UITableView 实例上调用时。

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

4

5 回答 5

6

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

setNeedsDisplayreloadData完全出于不同的目的。

于 2011-08-26T12:16:49.313 回答
5

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

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

有关该主题的更多信息,请参阅本文。

于 2011-08-26T12:17:13.733 回答
2

设置需求显示

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

重新加载数据

当 tableview 的基础数据源发生更改并且您希望在 UI 中反映它时必须调用它。在这种情况下调用此reloadData方法

于 2013-08-02T04:57:42.670 回答
1

Generally, you only need to call setNeedsDisplay on custom views for which you have implemented drawRect.

于 2011-08-26T15:32:46.790 回答
0

我的解决方案是更新所有可见单元格并使用 reloadRowsAtIndexPaths 在我的自定义类“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;
于 2016-04-26T07:07:45.500 回答