在didSelectRowAtIndexPath
我要更新tableviewcell并重新加载它。我能够重新加载,但内容没有得到更新。这是我从didSelectRowAtIndexPath
方法调用的代码。 self.visitorlistsTv
是我的桌面视图
-(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];
}
}
答案 0 :(得分:1)
简单的答案是,您更改UIButton图像的技术存在缺陷。而是使用:
[vcell.button setImage:[UIImage imageNamed:@"arrowdown.png"] forState:UIControlStateNormal];
但除此之外,解决这个问题的方法是在选择时更新模型,为该行调用reload并让你的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];
一个注意事项:两个分支上的重载代码是相同的,因此可以在条件之后将其重新计算。