[英]Disappearing UITableViewCell on UITableView reloadRowsAtIndexPaths:
我有一些從XIB文件加載的UITableViewCells。 除非單元格消失,否則在我調用[UITableView reloadRowsAtIndexPaths:withRowAnimation:]方法之前,一切都很好。 當我調用[UITableView reloadData]時,所有內容都會找到,當我在視圖上上下滾動該單元格時,它也會重新出現。 奇怪的。
我還注意到,當我調用[UITableView reloadRowsAtIndexPaths:withRowAnimation:]時,UITableView不會嘗試重用緩存的單元格,而是嘗試使用cell = [tableViewCells objectAtIndex:cellId]來獲得一個新的單元格。
這是我的代碼:
- (void)viewDidLoad
{
...
// the objects from the XIB files are loaded onto an NSArray instance variable at viewDidLoad
tableViewCells = [[NSBundle mainBundle] loadNibNamed:@"ProfileTableViewCell" owner:nil options:nil];
...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int cellId = /* some logic to get the correct cellID */
NSString *CellIdentifier = [NSString stringWithFormat:@"ProfileTableViewCell_%d", cellId];
ProfileTableViewCell *cell = (ProfileTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [tableViewCells objectAtIndex:cellId];
// additional work to setup subviews for the cell
[cell prepareSubviews];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
}
以防萬一,這是我在[ProfileTableViewCell prepareSubviews]中所做的一些事情
- (void)prepareSubviews
{
...
[self.containerView.layer setCornerRadius:3];
[self.containerView.layer setBorderColor:UIColorFromRGB(0xCDCDCD).CGColor];
[self.containerView.layer setBorderWidth:2.0];
[self.containerView.layer setMasksToBounds:YES];
[self.containerView.layer setShouldRasterize:NO];
[self.containerView.layer setRasterizationScale:[UIScreen mainScreen].scale];
...
}
在此先感謝能幫助我的出色人士。
我不確定這是否是您的問題,但是獲取細胞的方法不是正常的方法。 您應在自己的筆尖(具有唯一標識符)中制作每種類型的單元格,然后在registerNib:forCellReuseIdentifier:中注冊筆尖。 在cellForRowAtIndexPath中,只需根據索引路徑將所需的單元出隊,並且不要在if(cell == nil)子句中放入任何內容,因為用這種方式將不會調用該子句。
它縫接了reloadRowsAtIndexPaths:在只需要更改靜態單元格的高度時不起作用。
嘗試打電話給
[tableView beginUpdates];
[tableView endUpdates];
如文檔所述:
您還可以在不重新加載單元格的情況下,使用此方法以及endUpdates方法對行高的變化進行動畫處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.