首页 > 解决方案 > Swift - TableViewCell - 布局边距不起作用

问题描述

我有一个表格视图,应该显示他们的回复评论。每当显示回复时,左侧应该有一个空白,以便清楚地知道特定回复属于上面的评论。

为此,我尝试了以下操作:

class CommentsTableViewCell: UITableViewCell {

// label containing name of comment writer 
private func initSenderLabel() {
    senderLabel.translatesAutoresizingMaskIntoConstraints = false
    self.contentView.addSubview(senderLabel)
    senderLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 18).isActive = true
    senderLabel.widthAnchor.constraint(equalToConstant: 150).isActive = true
    senderLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 6).isActive = true
    senderLabel.heightAnchor.constraint(equalToConstant:14).isActive = true
    senderLabel.font = .boldSystemFont(ofSize: 14)
    senderLabel.adjustsFontSizeToFitWidth = true
    senderLabel.textColor = .gray
}

// actual comment
private func initCommentLabel() {
    commentLabel.translatesAutoresizingMaskIntoConstraints = false
    self.contentView.addSubview(commentLabel)
    commentLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 18).isActive = true
    commentLabel.widthAnchor.constraint(equalToConstant: 209).isActive = true
    commentLabel.topAnchor.constraint(equalTo: self.senderLabel.bottomAnchor, constant: 2).isActive = true
    commentLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -29).isActive = true
    commentLabel.font = .boldSystemFont(ofSize: 16)
    commentLabel.textColor = .black
    commentLabel.lineBreakMode = .byWordWrapping
    commentLabel.numberOfLines = 0
}

// button to load replies
private func initLoadReplyButton() {
    loadReplyButton.translatesAutoresizingMaskIntoConstraints = false
    self.contentView.addSubview(loadReplyButton)
    loadReplyButton.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 50).isActive = true
    loadReplyButton.widthAnchor.constraint(equalToConstant: 150).isActive = true
    loadReplyButton.topAnchor.constraint(equalTo: commentLabel.bottomAnchor, constant: 0).isActive = true
    loadReplyButton.heightAnchor.constraint(equalToConstant: 24).isActive = true
    loadReplyButton.addTarget(self, action: #selector(loadReplyButton_touched), for: .touchDown)
    loadReplyButton.setTitle(NSLocalizedString(LOAD_REPLIES_BUTTON_TITLE, comment: ""), for: .normal)
    loadReplyButton.backgroundColor = .clear
    loadReplyButton.setTitleColor(.darkGray, for: .normal)
}

// Sets up the cell using the comment data
func setup(comment: Comment, sender:String) {
    // Assign comment
    self.commentObject = comment
       
    // Update UI depending if it is a comment or reply
    if comment is CommentReply {            
        loadReplyButton.isHidden = true
        contentView.layoutMargins = UIEdgeInsets(top: 0, left: 150, bottom: 0, right: 0)
    }
    
    else {
        // Remove insets
        loadReplyButton.isHidden = false
           
        UIView.setAnimationsEnabled(false)
        updateButtonTitle()
        UIView.setAnimationsEnabled(true)
    }
    
    if !comment.hasReplies {
        loadReplyButton.isHidden = true
    }
        
    // Update size layout
    self.layoutIfNeeded()
}

但是,上面的代码仍然向我显示了“正常”评论的回复。如何修改回复的位置?

标签: swiftuitableviewtableviewcomments

解决方案


推荐阅读