【问题标题】:auto adjust custom UITableViewCell and Label in it to the text将其中的自定义 UITableViewCell 和 Label 自动调整为文本
【发布时间】:2016-10-24 10:58:19
【问题描述】:

我有一个自定义UITableViewCell,其中只有一个标签。我还在 StackOverflow 上阅读时将 Interface Builder 中的行数设置为 0。

import UIKit

class CommonListViewCell: UITableViewCell {
    @IBOutlet var background: UIView!
    @IBOutlet var titleLabel: UILabel!
    override func awakeFromNib() {

        super.awakeFromNib()
        // Initialization code
        titleLabel.font = UIFont(name: "roboto", size: titleLabel.font.pointSize)
        NSLog("CommonListViewCell font changed")
    }    
}

这是视图控制器与 UITableView 一起使用的部分:

override func viewDidLoad() {
    super.viewDidLoad()

    self.lvMain.delegate = self;
    self.lvMain.dataSource = self;
    self.lvMain.registerNib(UINib(nibName: "CommonListViewCell", bundle: nil), forCellReuseIdentifier: "commonlistidentifier")
    self.lvMain.estimatedRowHeight = 100
    self.lvMain.rowHeight =    UITableViewAutomaticDimension
}

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

    // Configure the cell...
    cell.titleLabel?.text = prayerList[indexPath.row].name
    cell.titleLabel?.sizeToFit()
    setLabelFont(cell.titleLabel!)
    Utils.nightCheck([cell.titleLabel!])
    return cell
}

所以,.sizeToFit() 并没有改变任何东西——它仍然使 1 行上的文本椭圆化,我需要标签尽可能多的行,并且单元格的大小也适合标签。

【问题讨论】:

  • 可以放标签截图吗?您想实现什么目标以及即将实现的目标?
  • 您是否在标签的所有侧面都设置了自动布局约束?并且不要在标签上设置约束高度。
  • @Jecky 我想要一个文本是否为 4 行 - 看到所有内容并且单元格与标签的文本一样高,如果它是 1 行 - 为 1 行高。现在它是 1 行,并且行在末尾用“..”进行椭圆化

标签: ios swift uitableview uilabel


【解决方案1】:

您需要使用自行调整大小的单元格。

tableView.estimatedRowHeight = 100
 tableView.rowHeight =    UITableViewAutomaticDimension

在故事板或 xib 中,您必须确保在单元格中设置适当的约束。 不要在标签中设置预定义的高度约束。只要确保给出顶部和底部约束即可。如果对文字不确定,也可以给标签一个最小高度约束。

【讨论】:

  • 对我没有帮助。标签只有边距限制。
  • @user2976267 你有没有试过让它自动调整大小,然后你必须确保单元格底部有底部约束
  • 我如何让它自我调整大小?我是 iOS 新手。是的,有底部约束。此外,标签在 IB 中为 0 行
  • 在 viewDidLoad 的答案中添加 tableview 代码,这告诉 tableview 不要从委托中寻找恒定高度,而是根据约束动态确定大小
  • 我编辑了帖子,看看 ViewController 部分。它仍然没有帮助我
【解决方案2】:

试试这个代码:

注意:这两种方法都应该有效。在 Swift 3 中测试。

方法一:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

tableView.estimatedRowHeight = 44.0 // standard tableViewCell height
tableView.rowHeight = UITableViewAutomaticDimension

return yourArrayName.count
}

方法二:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

注意:您需要将此代码放在您的 cellForRowAt 中

 yourCellName.sizeToFit()
 cell.textLabel?.numberOfLines = 0

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多