swift - TableView 单元格,在哪里设置选定单元格的样式?
问题描述
我在这个论坛上环顾四周,但没有找到任何相关的东西。
我想控制 UITableView 的选择功能:在选择和取消选择时设置单元格的样式,并在重用后保留它。
用户单击时的动作带有动画,TableViewController
当然在 's 文件中已清除:
func selectWorldMessage(indexPath: IndexPath) {
...
cell.attributedText = worldMessage.message.wholeWorldMessageAttributedString()
UIView.animate(withDuration: duration, animations: {
cell.bubbleImageView.tintColor = appColors.worldMessageBubbleSelected
cell.timeLabel.isHidden = false
cell.messageLabelBottomConstraint.constant = 14
cell.messageLabelTopConstraint.constant = 14
cell.timeLabel.alpha = 1.0
self.view.layoutIfNeeded()
self.tableView.beginUpdates()
self.tableView.endUpdates()
self.lastContentOffsetY = nil
}, completion: nil)
...
func deselectWorldMessage(indexPath: IndexPath) {
...
但是.. 假设您向下和向上滚动,单元格被重用。我必须重新设计它。我应该在哪里做?
A)在TableViewController
'cellForRowAt
函数中?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
....
// If previously was selected
if (currentSelectedIndexPath == indexPath) {
cell.messageLabel.attributedText = worldMessage.message.wholeWorldMessageAttributedString()
cell.bubbleImageView.tintColor = appColors.worldMessageBubbleSelected
cell.timeLabel.isHidden = false
cell.messageLabelBottomConstraint.constant = 14
cell.messageLabelTopConstraint.constant = 14
} else {
cell.messageLabel.attributedText = worldMessage.message.shortenWorldMessageIfNeededAttributedString()
cell.bubbleImageView.tintColor = appColors.worldMessageBubble
cell.timeLabel.isHidden = true
cell.messageLabelBottomConstraint.constant = 10
cell.messageLabelTopConstraint.constant = 10
}
B)还是在Cell
'ssetSelected
函数中?
override func setSelected(_ selected: Bool, animated: Bool) {
if selected == true {
self.messageLabel.attributedText = worldMessage.message.wholeWorldMessageAttributedString()
self.bubbleImageView.tintColor = appColors.worldMessageBubbleSelected
self.timeLabel.isHidden = false
self.messageLabelBottomConstraint.constant = 14
self.messageLabelTopConstraint.constant = 14
} else {
self.messageLabel.attributedText = worldMessage.message.shortenWorldMessageIfNeededAttributedString()
self.bubbleImageView.tintColor = appColors.worldMessageBubble
self.timeLabel.isHidden = true
self.messageLabelBottomConstraint.constant = 10
self.messageLabelTopConstraint.constant = 10
}
}
什么会更好、更省电?
解决方案
代码应该在您的单元格文件中,因为它是与单元格相关的东西,并且在重复使用后会保留下来。由您的视图控制器来处理单元格的“填充”逻辑,但在这种情况下,您希望更改单元格的 UI 样式。如果您将代码放在视图控制器中,当您更改单元格(通过添加或删除标签)时,您也必须更改控制器。您还需要保存选定的索引路径以签入 cellForRowAt 方法。
class TableViewCell: UITableViewCell {
override func setSelected(_ selected: Bool, animated: Bool) {
if selected {
self.backgroundColor = .red
} else {
self.backgroundColor = .green
}
}
}
推荐阅读
- reactjs - 如何在 JSX 中访问 Gatsby Create Page 模板 GraphQL 变量?
- apache - .htaccess 文件中“如果主机不是 foo,则重定向到 bar”的正确语法是什么?
- mongodb - 如何对位于mongodb对象下的嵌套数组中的数组进行排序
- python - 如何一起清除多个列表或字典
- javascript - 如何将两个变量存储在二维数组中添加数组传递传递ajax请求
- r - 如何在 dplyr 包中使用权重
- php - 在再次设置之前取消设置会话变量会导致使用时出现“未定义的索引”
- magento - Magento 2 将自定义属性附加到产品名称
- c++ - c++单例实现Meyer's vs call_once
- python - 将 Gnome Builder 与 Python 一起使用