1

Hi I am newbie of swift ios, how can I add custom UIView(s) with different height (depends on condition) into UITableViewCell one by one, and one below another. Just like the reminder app in iphone but each cell with different/same customViews - depends on condition. What's the best approach or practice for this. Thanks gurus.

eyc
  • 27
  • 6
  • 1
    Use Autolayout. http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift – derdida Aug 14 '15 at 07:42
  • If your cells will contain different views, you must create several cells (so many as you need), but if your cells vary by size, just make constraints to your cell's content view and use dynamic height as written below or rewrite the method heightForRowAtIndexPath: – Bogdan Matveev Aug 14 '15 at 08:13
  • Maybe I put it this way to be even more clear: TableViewCell -> contain a CustomView1 (either inherit UIView or UITableViewCell?), this CustomView1 may have UIImageView, TextBox, Label, etc.....Likewise, CustomView2 will have something similar but different, maybe just a textbox and with boarder, that is. so CustomView1 and CustomView2 will have different height for sure. Autolayout need to be done programatically or in xcode? I don't mind do it programatically, but how? – eyc Aug 14 '15 at 08:23
  • possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – Pascal Aug 14 '15 at 08:37

1 Answers1

1

To use custom views into UITableViewCell you have to add a xib file and add to a class extended of UITableViewCell.

To specify your custom view and UITablaView you must do it here:

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

Example:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("yourCellIdentifier") as! CustomView
    ...    
    return cell
}

To set auto height and more info you can follow this tutorial: dynamic table view cell height ios 8 swift

mcvasquez
  • 36
  • 1
  • 4