2016-08-16 93 views
2

我做了一個Collapsed TableView。根據內容,tableView標籤的大小不會增加。我已經設置了WordWrap和Lines = 0,但仍然無法正常工作。 我使用2個tableView單元格來製作摺疊視圖。TableView的textLabel高度根據內容

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

+0

是您使用自動佈局或自動尺寸在你的項目 –

+0

是的,我已經使用AutoLayout但不是標籤的。我在FirstCell的(即'header')按鈕上使用了它,它顯示了崩潰。第二個Cell完全空白。 – Nitesh

+0

看到這個https://www.hackingwithswift.com/read/32/2/automatically-resizing-uitableviewcells-with-dynamic-type-and-ns –

回答

0

試試這個代碼:

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 
} 
+0

我在閱讀Anbu.Karthik的評論後已經嘗試過,但之後只有摺疊單元格的textLabel高度已經修復。標題textLabel的高度保持不變。 – Nitesh

+0

編輯的代碼也無法正常工作。 – Nitesh

相關問題