所以我有一个带有2个原型单元的TableView。 这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == 0 { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ItemDetailsCell cell.centerImage.image = mainImage cell.heading.text = detailsHeadings[indexPath.row] let headingString = detailsHeadings[indexPath.row] cell.body.text = details[headingString] tableView.rowHeight = cell.labelBlack.frame.height + 40 return cell } else { let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! ItemDetailsCell cell.heading.text = detailsHeadings[indexPath.row] let headingString = detailsHeadings[indexPath.row] cell.body.text = details[headingString] let bodyString = details[headingString] let labelWidth = Int(cell.body.frame.width) println(labelWidth) let label = UILabel(frame: CGRect(x: 0, y: 0, width: labelWidth, height: 10000)) label.text = bodyString label.numberOfLines = 100 label.font = UIFont(name: "OpenSans-Light", size: 12.0) label.sizeToFit() tableView.rowHeight = label.frame.height + 3 return cell } }因此,第二个原型单元只有两个标签,其值由Dictionary分配。 根据有多少行文本,单元格大小需要扩展或收缩。 在自动布局中,我将行数设置为0,因此它会选择需要多行的行。 这可以正常工作,除非在应用程序中滚动时,它会在用户从底部向上滚动时捕捉视图。 有没有办法避免这种情况?
预先感谢您的帮助。
So I have a TableView with 2 prototype cells. Here is my code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == 0 { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ItemDetailsCell cell.centerImage.image = mainImage cell.heading.text = detailsHeadings[indexPath.row] let headingString = detailsHeadings[indexPath.row] cell.body.text = details[headingString] tableView.rowHeight = cell.labelBlack.frame.height + 40 return cell } else { let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! ItemDetailsCell cell.heading.text = detailsHeadings[indexPath.row] let headingString = detailsHeadings[indexPath.row] cell.body.text = details[headingString] let bodyString = details[headingString] let labelWidth = Int(cell.body.frame.width) println(labelWidth) let label = UILabel(frame: CGRect(x: 0, y: 0, width: labelWidth, height: 10000)) label.text = bodyString label.numberOfLines = 100 label.font = UIFont(name: "OpenSans-Light", size: 12.0) label.sizeToFit() tableView.rowHeight = label.frame.height + 3 return cell } }So the second prototype cell has just two labels with the values being assigned from a Dictionary. The cell size needs to expand or contract based upon how many lines of text there are. In auto layout I have the number of lines set to 0 so it will pick however many lines are needed. This works fine except when you scroll within the app it will snap the view up as users scroll back up from the bottom. Is there a way to avoid this?
Thanks in advance for any help.
最满意答案
在花更多时间寻找之后,我发现这实际上是:
http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
它给了我我需要的东西。 我删除了设置rowHeight的部分代码,然后使用viewDidLoad方法以及自动布局来约束我的单元格大小,并在经过一些试验和错误后进行工作,而无需在滚动时进行捕捉。
I found this actually after spending some more time looking:
http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
It gave me what I needed. I removed the parts of my code that set the rowHeight and then used the viewDidLoad method as well as auto layout to constrain my cell sizes and after a bit of trial and error it is working without snapping to place when you scroll.
更多推荐
发布评论