当viewWillAppear发生时,如何让我的UITableView单元始终== nil?我想完全重新加载我的所有单元格视图

时间:2022-04-10 22:47:28

How do I make my UITableView cells == nil (make them not cached) when viewWillAppear happens? I want to completely reload all my cell views every time I come to the view. The main reason is because the backgroundView image of the cells may changed because of "themes" I'm adding to the app.

当viewWillAppear发生时,如何使我的UITableView单元格== nil(使它们不被缓存)?我想在每次进入视图时完全重新加载所有单元格视图。主要原因是因为我正在添加到应用程序的“主题”,单元格的backgroundView图像可能会发生变化。

[tblView reloadData] doesn't work on the cached views on the cell.

[tblView reloadData]不适用于单元格上的缓存视图。

3 个解决方案

#1


1  

Your cellForRowAtIndexPath method in your table view controller probably has two lines like:

表视图控制器中的cellForRowAtIndexPath方法可能有两行,如:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

These lines attempt to reuse a previously created/formatted cell. To stop this reuse, take these lines out and instead just always allocate a new cell (i.e. something the equivalent of below).

这些行尝试重用先前创建的/格式化的单元格。要停止这种重用,请取出这些行,而只是总是分配一个新的单元格(即相当于下面的内容)。

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

Then when you do [tableView reloadData] in your viewWillAppear method it will always create brand new cells for each row rather than trying to reuse old ones.

然后,当您在viewWillAppear方法中执行[tableView reloadData]时,它将始终为每一行创建全新的单元格,而不是尝试重用旧的单元格。

#2


0  

Before you call reloadData, just let tableView:numberOfRowsInSection: return 0 first.

在调用reloadData之前,只需让tableView:numberOfRowsInSection:首先返回0。

Then your table will have no row at all.

然后你的桌子根本就没有排。

#3


0  

I would use -tableView:willDisplayCell:forRowAtIndexPath: to determine and set the background views of your cell right before their drawn on screen. Obviously you'll want to use your datasource to determine which background view should be displayed.

我会使用-tableView:willDisplayCell:forRowAtIndexPath:在屏幕上绘制之前确定并设置单元格的背景视图。显然,您需要使用数据源来确定应显示哪个背景视图。

#1


1  

Your cellForRowAtIndexPath method in your table view controller probably has two lines like:

表视图控制器中的cellForRowAtIndexPath方法可能有两行,如:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

These lines attempt to reuse a previously created/formatted cell. To stop this reuse, take these lines out and instead just always allocate a new cell (i.e. something the equivalent of below).

这些行尝试重用先前创建的/格式化的单元格。要停止这种重用,请取出这些行,而只是总是分配一个新的单元格(即相当于下面的内容)。

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

Then when you do [tableView reloadData] in your viewWillAppear method it will always create brand new cells for each row rather than trying to reuse old ones.

然后,当您在viewWillAppear方法中执行[tableView reloadData]时,它将始终为每一行创建全新的单元格,而不是尝试重用旧的单元格。

#2


0  

Before you call reloadData, just let tableView:numberOfRowsInSection: return 0 first.

在调用reloadData之前,只需让tableView:numberOfRowsInSection:首先返回0。

Then your table will have no row at all.

然后你的桌子根本就没有排。

#3


0  

I would use -tableView:willDisplayCell:forRowAtIndexPath: to determine and set the background views of your cell right before their drawn on screen. Obviously you'll want to use your datasource to determine which background view should be displayed.

我会使用-tableView:willDisplayCell:forRowAtIndexPath:在屏幕上绘制之前确定并设置单元格的背景视图。显然,您需要使用数据源来确定应显示哪个背景视图。