【问题标题】:Swift tableview cell auto height with auto height label带有自动高度标签的 Swift tableview 单元格自动高度
【发布时间】:2016-03-14 11:30:54
【问题描述】:

我对 swift 有一些问题。 需要制作 tableview 并在每个单元格中放置带有文本的图像。

这是我到目前为止所做的:

第一个问题:

标签应该是自动高度,现在它会破坏字符串..

第二个问题:

图像也需要自动高度,并且取决于标签高度。

第三题:

行需要根据其内部自动高度。

TableViewController 代码:

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.estimatedRowHeight = 44.0;
    tableView.tableFooterView = UIView(frame: CGRectZero)
    tableView.registerNib(UINib(nibName: "QuoteTableViewCell", bundle: nil), forCellReuseIdentifier: "QuoteCell")

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return results.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("QuoteCell", forIndexPath: indexPath) as! QuoteTableViewCell
    cell.item = results[indexPath.row]
    return cell
}

单元格代码:

class QuoteTableViewCell: UITableViewCell {

    var item: Quote! {
        didSet {
            setupCell()
        }
    }

    @IBOutlet weak var imageField: UIView!
    @IBOutlet weak var textField: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    func setupCell() {
        self.imageField.backgroundColor = UIColor(patternImage: UIImage(named: "Bg")!)
        textField.text = item.text
    }

}

【问题讨论】:

  • 你的 UILabel 和 UIImageView 有什么限制?
  • 我在图像视图上使用常量在顶部和底部设置间距,然后使用标签常量在该视图上设置间距。顺便说一句,我不使用 imageview,我使用 UIView,但它是一样的..我更新了我有常量的图像。
  • 您是否尝试在 UILabel 和 UI(Image)View 之间添加“等高”?我想如果你这样做应该没问题。应用文本后,还要在 UILabel 上调用 .sizeToFit() 。那么它应该更好地包裹文本的高度。
  • @TedHuinink 这行得通,但我失去了顶部和底部的填充,我需要很少的行之间的空白。解决方案?
  • 您是否在单元格的顶部和底部之间为 UITableviewCell 的直接子项(在这种情况下我认为是 UIView)添加了约束?因为它应该在正确设置时自动换行。

标签: swift tableviewcell uitableview


【解决方案1】:

要让标签根据其内容调整其高度,请在 Storyboard 中将其行数设置为 0。

如果你想在它旁边放一张图片,为什么不把两个控件放在一个水平的 StackView 中呢?

要让 tableView 行高适应单元格内容(即标签高度),只需将其 rowHeight 设置为自动:

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 90.0;

【讨论】:

    【解决方案2】:

    使用这个方法来拥有那个功能

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
          let lineBrakingMode =  NSLineBreakMode.ByCharWrapping
        let paragragh:NSMutableParagraphStyle = NSMutableParagraphStyle()
        paragragh.lineBreakMode = lineBrakingMode
         let attributes:NSDictionary = [NSFontAttributeName: self.textField.font!, NSParagraphStyleAttributeName:paragragh]
        let TextSize = self.textField.text!.sizeWithAttributes(attributes)
        let TextHeight = TextSize.height + 5;
    
         UIView.animateKeyframesWithDuration(0.2, delay: 2.0, options: UIViewKeyframeAnimationOptions.AllowUserInteraction, animations: { () -> Void in
            self.imageField.frame.height = TextHeight
            }, completion: nil)
        return TextHeight
    }
    

    }

    【讨论】:

    • Use of unresolved identifier 'paragragh' 和 Value of type 'LatestViewController' has no member 'textFeild' - 我使用 nib 中的单元格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2018-09-30
    相关资源
    最近更新 更多