10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

文字列の長さや大きさによりUILabelの表示領域が自動調整され、かつ、TableViewCellのheightもそれに合わせて自動調整する (iOS8以上)

Posted at

AutoLayout Settings

Screen Shot 2016-03-29 at 16.15.44.png

UITableViewCell

class DynamicHeightCell: UITableViewCell {
    @IBOutlet var label1: UILabel!
    @IBOutlet var label2: UILabel!
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // NOTE: If an app runs on iOS9 or more, these codes are unnecessary.
        self.label1.preferredMaxLayoutWidth = CGRectGetWidth(self.label1.bounds)
        self.label2.preferredMaxLayoutWidth = CGRectGetWidth(self.label2.bounds)
    }
    
    func setup(val1: String) {
        self.setup(val1: val1, val2: val1)
    }
    func setup(val1 val1: String, val2: String) {
        self.label1.text = val1
        self.label1.numberOfLines = 0 // Or, you can set it on a storyboard.
        self.label2.text = val2
        self.label2.numberOfLines = 0 // Or, you can set it on a storyboard.
        
        self.label1.hidden = "" == self.label1.text
        self.label2.hidden = "" == self.label2.text
    }
}

UITableView (or UITableViewController)

class TableViewHasDynamicHeightCellViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 160.0
    }
    
    // MARK: - Table view data source
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("TestCell") as! DynamicHeightCell
        
        let type = indexPath.row % 5
        
        switch type {
        case 0:
            cell.setup("TEST0")
            break
        case 1:
            cell.setup(val1: "TEST1", val2: "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめも あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめも あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめも あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめも あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめも あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめも わをん")
            break
        case 2:
            cell.setup(val1: "", val2: "TEST2")
            break
        case 3:
            cell.setup("あいうえお\nかきくけこ\nさしすせそ")
            break
        case 4:
            cell.setup(val1: "TEST4", val2: "")
            break
        default:
            cell.setup("TEST CELL is default.")
            break
        }
        
        return cell
    }
}

NOTE

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?