我正在使用tableview
和autolayout
。我想显示数组计数,增加故事板中的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
答案 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
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)仅将leading
,trailing
,top
和Height
约束应用于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()
在此之后,我没有看到实施UITableViewAutomaticDimention
和estimatedRowHeight
用 Swift 编写,将其改为目标C