在 didSelectRowAtIndexPath
上,我想更新 tableviewcell 并重新加载它。我可以重新加载,但内容没有得到更新。这是我从 didSelectRowAtIndexPath
方法调用的代码。 self.visitorlistsTv
是我的桌面 View
-(void) showDetails:(NSIndexPath *)indexPath {
if (! [expandedArray containsObject:indexPath]) {
[expandedArray addObject:indexPath];
// UPDATE THE IMAGE OF THE BUTTON
VisitorsListsCell *vcell = (VisitorsListsCell *) [self.visitorlistsTv cellForRowAtIndexPath:indexPath];
vcell.button.imageView.image = [UIImage imageNamed:@"arrowdown.png"];
// RELOAD
[self.visitorlistsTv beginUpdates];
[self.visitorlistsTv reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.visitorlistsTv endUpdates];
} else {
[expandedArray removeObject:indexPath];
// UPDATE THE BUTTOM IMAGE
VisitorsListsCell *vcell = (VisitorsListsCell *) [self.visitorlistsTv cellForRowAtIndexPath:indexPath];
vcell.button.imageView.image = [UIImage imageNamed:@"arrowup.png"];
// RELOAD THE DATA
[self.visitorlistsTv beginUpdates];
[self.visitorlistsTv reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.visitorlistsTv endUpdates];
}
最佳答案
简单的答案是您更改 UIButton 图像的技术有缺陷。而是使用:
[vcell.button setImage:[UIImage imageNamed:@"arrowdown.png"] forState:UIControlStateNormal];
但除此之外,解决此问题的方法是在选择时更新您的模型,为该行调用重新加载并让您的 cellForRowAt... 数据源完成工作。这样,即使单元格被丢弃并重新使用后,该行看起来也会正确。
换句话说,您在哪里执行此操作:
vcell.button.imageView.image = [UIImage imageNamed:@"arrowdown.png"];
...相反,向您的模型添加一些状态(图像的字符串名称,或表示向上与向下的 BOOL)并进行设置:
MyModelObject *modelObject = self.model[indexPath.row];
modelObject.arrowImageName = @"arrowdown.png"; // or the up arrow, depending on the condition
// now do your reload code
您的 cellForRowAt... 现在可以查找模型并使用以下方法更新出列的单元格:
MyModelObject *modelObject = self.model[indexPath.row];
UIImage *image = [UIImage imageNamed:modelObject.arrowImageName];
[cell.button setImage:image forState:UIControlStateNormal];
注意一点:两个分支上的重新加载代码是相同的,因此可以在条件之后将其分解。
关于iOS 更新 TableViewCell 内容并重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25144884/