【问题标题】:TableView's textLabel height according to contentTableView 的 textLabel 高度根据内容
【发布时间】:2016-08-16 09:21:50
【问题描述】:

我制作了一个折叠的 TableView。 tableView 的标签大小不会根据内容增加。我已经设置了 WordWrap 和 Lines = 0 但它仍然无法正常工作。 我正在使用 2 个 tableView 单元格来制作折叠视图。

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)
}

}

【问题讨论】:

  • 你在项目中使用了自动布局还是自动调整大小
  • 是的,我使用了 AutoLayout 但不是用于标签的。我在 FirstCell 的(即“标题”)按钮上使用了它,它显示正在折叠。第二个单元格完全空白。

标签: ios swift uitableview


【解决方案1】:

试试这个代码:

 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
}

【讨论】:

  • 我在阅读 Anbu.Karthik 的评论后已经尝试过了,但之后只有折叠单元格的 textLabel 的高度得到修复。标题 textLabel 的高度保持不变。
【解决方案2】:

更好的是这个答案:https://stackoverflow.com/a/29763200/1054550

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 2018-11-12
    • 2020-03-18
    相关资源
    最近更新 更多