iPhone - UITableViewCell 中的按钮不会更改图像
Posted
技术标签:
【中文标题】iPhone - UITableViewCell 中的按钮不会更改图像【英文标题】:iPhone - button in UITableViewCell does not change image 【发布时间】:2011-11-17 09:29:18 【问题描述】:我正在尝试使用 Xcode 4.2 故事板创建一些单元格,并使用 Interface Builder 创建一个自定义单元格。我的单元格创建得很好,里面有一个按钮,当我按下按钮时,我试图拥有它,图像发生了变化,但它似乎不起作用。这就是我所拥有的
- (IBAction)sendToFavorites:(id)sender
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
int row = [feedTableView indexPathForCell:cell].row;
NSLog(@"\"%@\" is FAVORITED", [[_allEntries objectAtIndex:row] articleTitle]);
if ([[_allEntries objectAtIndex:row] isFavorited])
NSLog(@"Unfavorite");
[[_allEntries objectAtIndex:row] setFavorite:NO];
[[sender imageView] setImage:[UIImage imageNamed:@"StarEmpty"]];
else
NSLog(@"Favorite");
[[_allEntries objectAtIndex:row] setFavorite:YES];
[[sender imageView] setImage:[UIImage imageNamed:@"StarFilled"]];
任何帮助将不胜感激。
【问题讨论】:
你得到日志了吗(不喜欢的/最喜欢的)? 是的,所有的 NSLog 都按照它们应该的方式打印。 另外,在切换图像时,它似乎会在一瞬间完成,但坚持使用 StarEmpty。我会是因为在 IB 中我将图像设置为 StarEmpty 吗? 【参考方案1】:您可以尝试刷新表格视图的内容。即,在设置与单元格关联的新图像后,在表格视图上调用 reloadData
。
据我从您的代码中了解到,您正在尝试直接修改单元格内容。相反,这应该通过控制表格重绘的dequeueReusableCellWithIdentifier
机制来实现。所以,很简单:修改你的数据源,然后重新绘制表格。
希望这会有所帮助。
【讨论】:
这不起作用,它似乎甚至看起来根本不像 UITableView 被改变了。无论如何,我都在努力避免这种情况。【参考方案2】:想通了。
[sender setImage:[UIImage imageNamed:@"StarEmpty"] forState:UIControlStateNormal];
不能单独设置,得用状态设置,不管是Highlighted、Normal等
【讨论】:
以上是关于iPhone - UITableViewCell 中的按钮不会更改图像的主要内容,如果未能解决你的问题,请参考以下文章
iPhone - 在 UITableViewCell 中移动 detailTextLabel
iPhone如何实现一个“宽”的UITableViewCell?
在 iPhone 中动态更改 UITableviewCell 高度?
UiTableViewCell 动态高度适用于 iphone 而不是 ipad 大小类