菜鸟教程小白 发表于 2022-12-11 17:16:39

ios - TableView 的 textLabel 高度根据内容


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

<pre><code>extension UIView {
func rotate(toValue: CGFloat, duration: CFTimeInterval = 0.2, completionDelegate: AnyObject? = nil) {
    let rotateAnimation = CABasicAnimation(keyPath: &#34;transform.rotation&#34;)
    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: !
    var collapsed: Bool!

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

var sections = ()

override func viewDidLoad() {
    super.viewDidLoad()

    sections = )]
}

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    return (sections.collapsed!) ? 0 : sections.items.count
}

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -&gt; UIView? {
    let header = tableView.dequeueReusableCellWithIdentifier(&#34;header&#34;) as! CollapsibleTableViewHeader

    header.toggleButton.tag = section
    header.titleLabel.text = sections.name
    header.toggleButton.rotate(sections.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) -&gt; UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(&#34;cell&#34;) as UITableViewCell!

    cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
    cell.textLabel?.numberOfLines = 0
    cell.textLabel?.text = sections.items



    return cell
}

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

    // Toggle collapse
    sections.collapsed = !collapsed

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

}
</code></pre>

<p> <a href="/image/0XJkG.png" rel="noreferrer noopener nofollow"><img src="/image/0XJkG.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试这个代码:</p>

<pre><code> func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -&gt; CGFloat {

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

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -&gt; UIView? {
    var lblSectionName: UILabel = UILabel()
    lblSectionName.text = self.sectionNames
    lblSectionName.numberOfLines = 0
    lblSectionName.lineBreakMode = .ByWordWrapping
    lblSectionName.sizeToFit()
    return lblSectionName
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - TableView 的 textLabel 高度根据内容,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38971081/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38971081/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - TableView 的 textLabel 高度根据内容