首页 > 解决方案 > 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
        }

    }

什么会更好、更省电?

标签: swiftselecttableviewcell

解决方案


代码应该在您的单元格文件中,因为它是与单元格相关的东西,并且在重复使用后会保留下来。由您的视图控制器来处理单元格的“填充”逻辑,但在这种情况下,您希望更改单元格的 UI 样式。如果您将代码放在视图控制器中,当您更改单元格(通过添加或删除标签)时,您也必须更改控制器。您还需要保存选定的索引路径以签入 cellForRowAt 方法。

class TableViewCell: UITableViewCell {
    override func setSelected(_ selected: Bool, animated: Bool) {
        if selected {
            self.backgroundColor = .red
        } else {
            self.backgroundColor = .green
        }
    }
}

推荐阅读