簡體   English   中英

根據label.text更改TableViewCell的高度?

[英]Change TableViewCell height depending on label.text?

如何更改單元格高度以適合UILabel? 我沒有在項目中使用自動版式。

另外,在cellForRowAtIndexPath設置TableViewCell文本。

碼:

var commentsArray: [String] = []

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell:TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell;

        if self.commentsArray.count > indexPath.row{
            cell.commentsText.text = commentsArray[commentsArray.count - 1 - indexPath.row]
            cell.commentsText.font = UIFont.systemFontOfSize(15.0)
        }

        return cell
    }

有任何想法嗎?

不使用自動布局時,上面使用heightForRowAtIndexPath的注釋是可以接受的(盡管我強烈建議您使用它)。

可以使用本文中介紹的方法來計算標簽中填充有特定字符串的高度如何動態計算UILabel高度?

1)您可以使用一種方法來設置約束,並且可以通過以下方式修改單元格的高度 :( 示例在自定義UITableViewCell中帶有名為book_title的標簽)

fune setupComponents(){

self.addSubview(book_title)

book_title.topAnchor.constraintEqualToAnchor(self.topAnchor, constant: 5).active = true
        book_title.leftAnchor.constraintEqualToAnchor(self.leftAnchor, constant: 10).active = true
        book_title.rightAnchor.constraintEqualToAnchor(self.rightAnchor).active = true
        book_title.widthAnchor.constraintEqualToAnchor(self.widthAnchor, constant: -20).active = true
        book_title.heightAnchor.constraintEqualToConstant(90).active = true
        book_title.numberOfLines = 0
        book_title.lineBreakMode = NSLineBreakMode.ByWordWrapping

}

2)您必須在以下方法中調用此方法:

 required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        setupComponents()
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Subtitle, reuseIdentifier: "CellId")

        setupComponents()

    }

暫無
暫無

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

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