如何在选择 UITableView 行时更改其颜色?
我已经四处寻找这个问题的答案,但尚未找到有效的答案。我想要做的就是能够更改背景颜色或我选择的 UITableView 行的图像。
我习惯于在选择一行时转换视图,但这次我希望能够更改该行属性。我知道我想使用 indexPath.row 更改哪一行,但我不知道如何更改该行的外观,因为 cellForRowAtIndexPath 方法已经被调用!
我可以做什么来改变颜色?
I've looked around for answers to this but have yet to find one that works. All I want to do is to be able to change the background color, or image of the UITableView row that I select.
I am used to just transitioning views when you I select a row, but this time I want to be able to change that rows properties. I know which row I want to change, with indexPath.row, but I don't know how I would go about changing how the row looks because the cellForRowAtIndexPath method has already been called!
What can I do to change the color?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
只要确保在回收细胞时考虑到这种颜色变化即可。
如果您需要更多信息,请发表评论。
You would do it like this:
Just make sure you consider this color change when you recycle cells.
If you need more info just post a comment.
将变量设置为活动行:
构建单元格时,检查它是否是活动行并设置背景颜色
即使在正常情况下也不要忘记设置背景颜色,因为它们会被重用。
在选择时(或在适当的时候),更新该特定单元格:
我建议使用此方法,因为可能存在导致表格刷新的事件,并且您可能不希望丢失背景颜色。如果您直接在选择时设置单元格,则可能会发生这种情况。
Set a variable to the active row:
When building the cell check if it's the active row and set the background color
Don't forget to set the background color even on the normal case, since they are reused.
On selection (or whenever is appropriate), update that particular cell:
I would suggest this method as there may be events that cause the refresh of your table, and you may not want to lose the background color. This could happen if you just set the cell directly on selection.