更改 UIButton 的 setHighlighted
我正在使用 UIButton 来显示表格单元格项目并将其从已读切换为未读。 我使用蓝色图像作为背景来显示项目未读的时间,这很有效,但我认为最好使用 setHighlighted 功能,因为它可以节省向应用程序添加额外图像的麻烦。我使用的代码如下:
- (void)updateReadButton{
if(article.read.boolValue){
//[readButton setBackgroundImage:nil forState:UIControlStateNormal];
[readButton setHighlighted:FALSE];
[readButton setNeedsLayout];
} else {
[readButton setHighlighted:TRUE];
[readButton setNeedsLayout];
}
}
这对于单元的初始创建来说效果很好。但是,当单击该项目并显示详细信息视图时,我将“读取”值切换为“true”,并将单元格中 UIButton 的 setHighlighted 选项更改为 false,但从详细信息返回时它不会改变看法。仅当单元格滚动离开屏幕并重新创建时,才会反映更改。导航到详细视图后如何强制重绘按钮?
I am using an UIButton to show and toggle a table-cell-item from read to unread.
I used a blue image as a background to show when an item was unread, and that worked, but figured that it would be better to use the setHighlighted feature, as it saves adding an extra image to the App. The code I use is as follows:
- (void)updateReadButton{
if(article.read.boolValue){
//[readButton setBackgroundImage:nil forState:UIControlStateNormal];
[readButton setHighlighted:FALSE];
[readButton setNeedsLayout];
} else {
[readButton setHighlighted:TRUE];
[readButton setNeedsLayout];
}
}
This works fine for the initial creation of the cell. But when the item is clicked and the detail-view is shown I toggle the 'read' value to 'true' and change the setHighlighted option to false of the UIButton in the cell, but it doesn't change when coming back from the detail view. Only when the cell is scrolling off the screen and recreated is the change reflected. How can I force a redraw of the button once I navigate to the detail view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过简单地重新加载相关单元格来完成突出显示。查看 UITableView 的
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
并在设置突出显示后使用它重新加载单元格。You may be able to accomplish the highlight by simply reloading the cell in question. Peek at UITableView's
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
and use it to reload the cell after setting the highlight.我认为
setHighlighted:
方法不是正确的。这是文档:I don't think the method
setHighlighted:
is the correct one. Here is the documentation: