iOS 7的动态表格视图单元格高度(Dynamic Table view cell height for iOS 7)

编程入门 行业动态 更新时间:2024-10-28 14:23:08
iOS 7的动态表格视图单元格高度(Dynamic Table view cell height for iOS 7)

我有一个包含tableview的视图控制器。 表视图由包含单个标签的自定义单元格组成。 我有不同长度的文本,将在这些单元格中显示。 我面临的问题是,细胞没有扩展到适当的高度。 我在SO中尝试了很多解决方案,但到目前为止它们都没有。 这是视图控制器的代码

class ViewController: UIViewController { @IBOutlet var tableView: UITableView! let items = [ "This is the first text", "This is the first text and this is the second text","now you may be thinking where is the third text?. Well, There was the first text and second text, now here is the third text", "This is the fourth short and sweet text", "Slow down you crazy child, you're so ambitious for a juvenile. If you're so smart, tell me why are you still so afraid.","Where's the fire? What's the hurry about. You better cool it off before you burn it out. There's so much to do and so many hours in a day.You got your passion, got your pride. Don't you know that only fools are satisfied. Dream on but don't imagine that they come true. Don't even realise vienna waits for you"] var prototypeCell:CustomCell! override func viewDidLoad() { super.viewDidLoad() //setting up tableview self.tableView.allowsSelection = false self.tableView.dataSource = self self.tableView.delegate = self configurePrototypeCell() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func configureCell(cell:CustomCell, forIndexPath indexPath:NSIndexPath) { cell.itemLabel.text = items[indexPath.row] } func configurePrototypeCell() { self.prototypeCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell } } extension ViewController:UITableViewDataSource { func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell configureCell(cell, forIndexPath: indexPath) return cell } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } } extension ViewController: UITableViewDelegate { func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { self.prototypeCell.itemLabel.text = items[indexPath.row] self.prototypeCell.itemLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.tableView.frame) self.prototypeCell.setNeedsLayout() self.prototypeCell.layoutIfNeeded() let size = self.prototypeCell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) let height = size.height return height } func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } }

CustomCell类是UITableViewCell子类。 它包含名为itemLabel的UILabel 。

I have a view controller containing tableview. Table view consists of custom cell containing single label. I have text of varying length which are to be shown in those cells. The problem i am facing is that, the cells are not getting expanded to the appropriate height. I have tried many solutions present in SO but none of them are working so far. Here is the code for view controller

class ViewController: UIViewController { @IBOutlet var tableView: UITableView! let items = [ "This is the first text", "This is the first text and this is the second text","now you may be thinking where is the third text?. Well, There was the first text and second text, now here is the third text", "This is the fourth short and sweet text", "Slow down you crazy child, you're so ambitious for a juvenile. If you're so smart, tell me why are you still so afraid.","Where's the fire? What's the hurry about. You better cool it off before you burn it out. There's so much to do and so many hours in a day.You got your passion, got your pride. Don't you know that only fools are satisfied. Dream on but don't imagine that they come true. Don't even realise vienna waits for you"] var prototypeCell:CustomCell! override func viewDidLoad() { super.viewDidLoad() //setting up tableview self.tableView.allowsSelection = false self.tableView.dataSource = self self.tableView.delegate = self configurePrototypeCell() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func configureCell(cell:CustomCell, forIndexPath indexPath:NSIndexPath) { cell.itemLabel.text = items[indexPath.row] } func configurePrototypeCell() { self.prototypeCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell } } extension ViewController:UITableViewDataSource { func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell configureCell(cell, forIndexPath: indexPath) return cell } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } } extension ViewController: UITableViewDelegate { func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { self.prototypeCell.itemLabel.text = items[indexPath.row] self.prototypeCell.itemLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.tableView.frame) self.prototypeCell.setNeedsLayout() self.prototypeCell.layoutIfNeeded() let size = self.prototypeCell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) let height = size.height return height } func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } }

CustomCell class is subclass of UITableViewCell. It contains UILabel named itemLabel.

最满意答案

请在自定义单元格中尝试此操作,从TableviewDelegate高度方法调用此方法来计算自定义单元格高度。

码: let size = sizingCell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)

Please try this in custom cell call this from TableviewDelegate method of height to calculate custom cell height.

Code: let size = sizingCell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)

更多推荐

本文发布于:2023-08-03 07:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   单元格   表格   高度   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!