auto adjust custom UITableViewCell and Label in it to the text

橙三吉。 提交于 2019-12-18 07:17:26

问题


I have a custom UITableViewCell that only has one Label in it. I also set number of lines in Interface Builder to 0 as I read on StackOverflow.

import UIKit

class CommonListViewCell: UITableViewCell {
    @IBOutlet var background: UIView!
    @IBOutlet var titleLabel: UILabel!
    override func awakeFromNib() {

        super.awakeFromNib()
        // Initialization code
        titleLabel.font = UIFont(name: "roboto", size: titleLabel.font.pointSize)
        NSLog("CommonListViewCell font changed")
    }    
}

Here's the view controller's part to work with UITableView:

override func viewDidLoad() {
    super.viewDidLoad()

    self.lvMain.delegate = self;
    self.lvMain.dataSource = self;
    self.lvMain.registerNib(UINib(nibName: "CommonListViewCell", bundle: nil), forCellReuseIdentifier: "commonlistidentifier")
    self.lvMain.estimatedRowHeight = 100
    self.lvMain.rowHeight =    UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("commonlistidentifier", forIndexPath: indexPath) as! CommonListViewCell

    // Configure the cell...
    cell.titleLabel?.text = prayerList[indexPath.row].name
    cell.titleLabel?.sizeToFit()
    setLabelFont(cell.titleLabel!)
    Utils.nightCheck([cell.titleLabel!])
    return cell
}

So, .sizeToFit() doesn't change anything—it still ellipsize the text on 1 line, and I need the label to be as many lines as it needed, and the size of the cell also be fitting for the label.


回答1:


You need to use self sizing cells.

tableView.estimatedRowHeight = 100
 tableView.rowHeight =    UITableViewAutomaticDimension

In your storyboard or xib you have to make sure that you set proper constraints in your cells. Do not set a predefined height constraint in your label. Just make sure you give top and bottom constraints. If you are not sure about the text , you can also give a minimum height constraint to the label.




回答2:


Try this code:

Note: Both method should work.Tested in Swift 3.

Method 1:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

tableView.estimatedRowHeight = 44.0 // standard tableViewCell height
tableView.rowHeight = UITableViewAutomaticDimension

return yourArrayName.count
}

Method 2:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

Note: You need to put this code inside your cellForRowAt

 yourCellName.sizeToFit()
 cell.textLabel?.numberOfLines = 0

Output:



来源:https://stackoverflow.com/questions/40216729/auto-adjust-custom-uitableviewcell-and-label-in-it-to-the-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!