将UITableView的附件视图设置为UILabel

时间:2018-01-24 05:56:30

标签: ios swift uitableview

我正在尝试将UITableView的附件视图设置为UILabel。我试过在互联网上寻找答案,但找不到任何东西。这是我的工作:

let listCountLabel: UILabel = {
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
    label.text = "1"
    label.layer.borderWidth = 2
    label.layer.borderColor = UIColor.lightGray.cgColor
    label.textColor = .lightGray
    label.layer.cornerRadius = label.frame.height / 2
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont(name: "HelveticaNeue", size: 14)
    label.textAlignment = .center
    return label
}()


//Add to Subview
view.addSubview(listCountLabel)


cell.accessoryView = listCountLabel

1 个答案:

答案 0 :(得分:0)

无需调用view.addSubView()。只需将listCountLabel.sizeToFit()插入到cell.accessoryView中即可。

listCountLabel.sizeToFit() 
cell.accessoryView = listCountLabel