在storyboard swift中设置动态tableview高度约束

时间:2017-02-07 04:35:40

标签: swift

我正在使用tableviewautolayout。我想显示数组计数,增加故事板中的tableview高度。怎么做?

我正在使用以下代码但不能正常工作

@property(strong,nonatomic)iboutlet nslayoutconstraint *dynamicheight;

self.dynamicheight.constant=[self.array count]*44

请帮助我这个人吗?

https://i.stack.imgur.com/jadT7.png  https://i.stack.imgur.com/dP3tZ.png

3 个答案:

答案 0 :(得分:0)

使用以下

@IBOutlet weak var tableView: UITableView! //connect the table view

//do following in viewDidLoad method
tableView.estimatedRowHeight = 100 //Your estimated height
tableView.rowHeight = UITableViewAutomaticDimension

答案 1 :(得分:0)

试试这个 -

如果您使用tableView单元格,请在cellForRowAt方法

中添加以下代码
  cell.textLabel?.numberOfLines = 0
  cell.textLabel?.lineBreakMode = .byWordWrapping

并在viewDidLoad

中设置UITableView的这些属性
self.tableView.tableFooterView = UIView()
self.tableView.estimatedRowHeight = 120 //Your estimated height
self.tableView.rowHeight = UITableViewAutomaticDimension

希望它能奏效:)

检查以下完整代码:

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var guestTView: UITableView!

var guests: [String]?

override func viewDidLoad() {
    super.viewDidLoad()
    self.guests = ["hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsgg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg"]

    self.guestTView.estimatedRowHeight = 70.0
    self.guestTView.rowHeight = UITableViewAutomaticDimension
    self.guestTView.tableFooterView = UIView()

}

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

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (guests?.count)!
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.numberOfLines = 0
    cell.textLabel?.lineBreakMode = .byWordWrapping
    cell.textLabel?.text = guests?[indexPath.row]

    return cell

 }

}

答案 2 :(得分:0)

如果您希望tableView height动态,请按以下步骤操作:

1)仅将leadingtrailingtopHeight约束应用于tableview。

2)删除您已应用的bottom约束。

3)将height约束的出口作为:

 @IBOutlet weak var heightConstraint: NSLayoutConstraint!

4)在viewWillAppear()中将dynamicHeight约束更改为:

let rowCount = self.array.count
self.dynamicHeight.constant = CGFloat(rowCount) * 44
view.updateConstraints()
self.yourTableView.reloadData()

在此之后,我没有看到实施UITableViewAutomaticDimentionestimatedRowHeight

的任何意义

Swift 编写,将其改为目标C