iOS Swift更改TableviewCell

时间:2018-11-02 16:56:51

标签: swift uitableview padding collectionview

您好,我正在尝试制作气泡UITableviewCell。该单元格有一个UILabel,所有edges引脚都连接到单元格ContentView的边缘,并且UITableView正在使用AutomaticHeight(基于Label.text)将CellBackgroundView图片设置为以下功能

func changeImage(_ name: String) -> UIImage? {
        guard let image = UIImage(named: name) else { return nil }
        return image.resizableImage(withCapInsets: UIEdgeInsets(top: 17, left: 30, bottom: 17, right: 30), resizingMode: .stretch).withRenderingMode(.alwaysTemplate)
    } 

cellforRowAtIndexPath

中调用以上函数
cell.backgroundView = UIImageView(image: changeImage("right_bubble"))

我的背景图片看起来像气泡

enter image description here

问题是我无法更改padding的左或右Cell。是否可以更改填充(可以向左或向右)(以区分发送方和接收方)还是需要从UITableView更改为UICollectionView

编辑:我已经提出了一个解决方案,可以在Custom Cell子类中动态更改约束。贝娄是子类的实现

import UIKit

enum Direction {
    case left
    case right
}

class MessageCell: UITableViewCell {

    @IBOutlet weak var backgroundImageView: UIImageView!

    @IBOutlet weak var messageText: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func prepareForReuse() {
        self.layoutIfNeeded()
    }

    func configureCell(with message: Message, direction: Direction ) {
        self.messageText?.text = message.text

        switch direction {
        case .left:
            backgroundImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10).isActive = true
            contentView.trailingAnchor.constraint(greaterThanOrEqualTo: backgroundImageView.trailingAnchor, constant: 50).isActive = true
            self.backgroundImageView.image = UIImage.changeImage("left_bubble")
            self.backgroundImageView.tintColor = .lightGray
        case .right:
            backgroundImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -10).isActive = true
            backgroundImageView.leadingAnchor.constraint(greaterThanOrEqualTo: contentView.leadingAnchor, constant: 50).isActive = true
            self.backgroundImageView.image = UIImage.changeImage("right_bubble")
            self.backgroundImageView.tintColor = .blue
        }
        self.layoutIfNeeded()
    }

}

,但是当行数超过屏幕高度并需要滚动时,会遇到Autolayout的问题。波纹管是错误的

2018-11-03 19:53:36.478275+0800 VicGithubDM[2352:34611] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x6000006e9090 V:|-(10)-[UILabel:0x7fe37dd3bb70'still have some bugs stil...']   (active, names: '|':UITableViewCellContentView:0x7fe37dd3b750 )>",
    "<NSLayoutConstraint:0x6000006ea620 V:[UILabel:0x7fe37dd3bb70'still have some bugs stil...']-(10)-|   (active, names: '|':UITableViewCellContentView:0x7fe37dd3b750 )>",
    "<NSLayoutConstraint:0x6000006e1d10 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe37dd3b750.height == 1.19209e-07   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000006ea620 V:[UILabel:0x7fe37dd3bb70'still have some bugs stil...']-(10)-|   (active, names: '|':UITableViewCellContentView:0x7fe37dd3b750 )>  

1 个答案:

答案 0 :(得分:0)

我建议创建一个自定义单元格并根据类型更改约束值。