簡體   English   中英

Swift-以編程方式自定義UITableViewCell

[英]Swift - custom UITableViewCell programmatically

我在自定義UITableView內的cells時遇到麻煩。 我想在左側有一個UIButton ,但是不知何故, UIButton只出現在第一個cell

在此處輸入圖片說明

這就是我在UITableViewCell內部創建UIButton

class WhishCell: UITableViewCell {

let checkButton: UIButton =  {
    let v = UIButton()
    v.backgroundColor = .red
    v.translatesAutoresizingMaskIntoConstraints = false
    return v
}()



public static let reuseID = "WhishCell"

required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.addSubview(checkButton)
    self.backgroundColor = .clear

}
}

我的TableView

class WhishlistTableViewController: UITableViewController {

public var wishList = [Wish]()


override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.allowsSelection = false
    self.tableView.register(WhishCell.self, forCellReuseIdentifier: WhishCell.reuseID)
    self.wishList.append(Wish(withWishName: "Test"))

}

// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return wishList.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: WhishCell.reuseID, for: indexPath)
    let currentWish = self.wishList[indexPath.row]
    cell.textLabel?.text = currentWish.wishName
    cell.backgroundColor = .clear

    return cell
}

有誰知道為什么它只出現在第一個cell TableViews有點新,所以我對每個幫助都很滿意:)

正如其他人所暗示的,原因是您從未為按鈕定義布局約束。 這是您的init的示例更新,其中包括布局,盡管您可以通過多種方法進行此操作。

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  super.init(style: style, reuseIdentifier: reuseIdentifier)
  self.addSubview(checkButton)
  self.checkButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
  self.checkButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
  self.checkButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
  self.checkButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  self.backgroundColor = .clear
}

當然,我只是在這里限制條件,但這可以實現您想要的。

UIButton是UIView的子類,因此UIbutton的初始化需要框架或約束來指定其位置。 嘗試將約束應用於checkButton將解決您的問題。

您應該向UIButton添加約束,因為父視圖需要知道按鈕必須在哪里。 另外,如果視圖內部還有其他組件,則必須聲明它。

總之,您必須告訴組件要添加的組件。

試試這個

self.addConstraint(NSLayoutConstraint(item: checkButton, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 10))
self.addConstraint(NSLayoutConstraint(item: checkButton, attribute: .trailing, relatedBy: .equal, toItem: textLabel, attribute: .trailing, multiplier: 1, constant: 10))
self.addConstraint(NSLayoutConstraint(item: checkButton, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 10))
self.addConstraint(NSLayoutConstraint(item: checkButton, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 10))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM