可调整大小的TableView单元格

时间:2016-08-22 01:55:16

标签: ios swift uitableview

所以我一直在尝试创建一个通用的聊天bubble-ish外观,在UITableView单元格中有一个可调整大小的视图和标签。事情进展顺利,直到我尝试添加可调整大小的功能。它只是在底部切断了一点(或没有任何余量),我之前没有像这样完全动态的单元格,所以我不知道如何解决这个问题。我尝试添加20px缓冲区但它没有帮助。我很感激帮助! (以下代码

Simulator screenshot of tableview

import QuartzCore
import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var listOfStrings = [String] ()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

     // When I Uncomment the 2 lines below, the cell cuts off and only displays a little bit of the blue view.
  //  self.tableView.rowHeight = UITableViewAutomaticDimension
  //         self.tableView.estimatedRowHeight = 75
        listOfStrings.append("Switch, Button, Segmented Control, Slider, Textfield")
        listOfStrings.append("Switch, Button, Segmented Control, Slider, Textfield")
        listOfStrings.append("Switch, Button, Segmented Control, Slider, Textfield")
    }


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(indexPath.row)
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("someCell") as! SomeTableViewCell

        cell.contentView.viewWithTag(0)!.backgroundColor = UIColor.clearColor()
        let size = cell.layer.bounds
        let tableSize = self.tableView.layer.bounds
        let viewCGR = CGRect(x: size.minX, y: size.height/2, width: tableSize.width, height: size.height/2)
        let view: UIView = UIView(frame: viewCGR)

        let labelCGR = CGRect(x: 0, y: 0, width: viewCGR.width, height: viewCGR.height)
        let label: UILabel = UILabel(frame: labelCGR)
        label.numberOfLines = 0

        label.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)


        label.textAlignment = NSTextAlignment.Center
        label.text = listOfStrings[indexPath.row]
        label.sizeToFit()
        self.tableView.updateConstraints()


        let newViewCGR = CGRect(x: viewCGR.minX, y: viewCGR.minY, width: label.frame.width+20, height: label.frame.height+20)
        view.frame = newViewCGR
        view.sizeToFit()
        label.textColor = UIColor.blackColor()
         label.center.x = view.center.x
       //  self.tableView.updateConstraints()
        view.addSubview(label)
        self.tableView.updateConstraints()
        view.backgroundColor = UIColor.blueColor()
        view.layer.cornerRadius = 6
        cell.addSubview(view)
         self.tableView.updateConstraints()
        return cell

    }

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

顺便说一下,我没有使用自动布局+约束来获得让气泡能够出现在左侧或右侧(取决于传入或传出)的功能。

3 个答案:

答案 0 :(得分:0)

在Apple推出新API之后,您无法计算身高。你只需使用autolayout来完成工作。我认为this link可以帮到你很多。顺便说一下,有一个很好的教程Self-sizing Table View Cells

答案 1 :(得分:0)

您不必为高度单元格计算高度行。 您在viewDidLoad()

中添加此代码
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 50

答案 2 :(得分:0)

@Ryan使用以下方法查找单元格的高度,

  CGRect textRect =
  [attributedText boundingRectWithSize:CGSizeMake(cellWidth, CGFLOAT_MAX)
  options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
  context:nil];

调用此方法
  

heightForRowAtIndexPath:

并将单元格的高度设置为textRect.size.height

相关问题