2

I have made an Collapsed TableView. The size of the tableView's label is not increasing according the content. I had set WordWrap and Lines = 0 but still it's not working. I'm using 2 tableView cell's to make the collapsed view.

extension UIView {
func rotate(toValue: CGFloat, duration: CFTimeInterval = 0.2, completionDelegate: AnyObject? = nil) {
    let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
    rotateAnimation.toValue = toValue
    rotateAnimation.duration = duration
    rotateAnimation.removedOnCompletion = false
    rotateAnimation.fillMode = kCAFillModeForwards

    if let delegate: AnyObject = completionDelegate {
        rotateAnimation.delegate = delegate
    }
    self.layer.addAnimation(rotateAnimation, forKey: nil)
}
}

class CollapsibleTableViewController: UITableViewController {
struct Section {
    var name: String!
    var items: [String]!
    var collapsed: Bool!

    init(name: String, items: [String], collapsed: Bool = false) {
        self.name = name
        self.items = items
        self.collapsed = collapsed
    }
}

var sections = [Section]()

override func viewDidLoad() {
    super.viewDidLoad()

    sections = [Section(name: "TEXT OVER HERE", items: ["TEXT OVER HERE."])]
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (sections[section].collapsed!) ? 0 : sections[section].items.count
}

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = tableView.dequeueReusableCellWithIdentifier("header") as! CollapsibleTableViewHeader

    header.toggleButton.tag = section
    header.titleLabel.text = sections[section].name
    header.toggleButton.rotate(sections[section].collapsed! ? 0.0 : CGFloat(M_PI_2))
    header.toggleButton.addTarget(self, action: #selector(CollapsibleTableViewController.toggleCollapse), forControlEvents: .TouchUpInside)

    return header.contentView
}

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

    cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
    cell.textLabel?.numberOfLines = 0
    cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]



    return cell
}

//
// MARK: - Event Handlers
//
func toggleCollapse(sender: UIButton) {
    let section = sender.tag
    let collapsed = sections[section].collapsed

    // Toggle collapse
    sections[section].collapsed = !collapsed

    // Reload section
    tableView.reloadSections(NSIndexSet(index: section), withRowAnimation: .Automatic)
}

}

enter image description here

Nitesh
  • 1,564
  • 2
  • 26
  • 53
  • are you used autolayout or autoresizing in your project – Anbu.Karthik Aug 16 '16 at 09:22
  • Yes, I have used AutoLayout but not for the Label's. I have used it on FirstCell's(i.e 'header') Button which shows collapsing. The second Cell is completely blank. – Nitesh Aug 16 '16 at 09:28
  • see this https://www.hackingwithswift.com/read/32/2/automatically-resizing-uitableviewcells-with-dynamic-type-and-ns – Anbu.Karthik Aug 16 '16 at 09:31

2 Answers2

0

try this code:

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    var lblSectionName: UILabel = UILabel()
    lblSectionName.text = self.sectionNames[section]
    lblSectionName.numberOfLines = 0
    lblSectionName.lineBreakMode = .ByWordWrapping
    lblSectionName.sizeToFit()
    return lblSectionName.frame.size.height
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var lblSectionName: UILabel = UILabel()
    lblSectionName.text = self.sectionNames[section]
    lblSectionName.numberOfLines = 0
    lblSectionName.lineBreakMode = .ByWordWrapping
    lblSectionName.sizeToFit()
    return lblSectionName
}
Chirag Patel
  • 1,453
  • 1
  • 11
  • 21
  • I tried that already after reading Anbu.Karthik's comment, but after that only the Collapsed cell's textLabel's height is getting fixed. The header textLabel's height remains same as it is. – Nitesh Aug 16 '16 at 09:42
0

Better is this answer: https://stackoverflow.com/a/29763200/1054550

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;
goodliving
  • 176
  • 2
  • 6
  • 8