我正在使用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
试试这个-
如果您正在使用tableView单元格,则在cellForRowAt
方法中添加以下代码
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = .byWordWrapping
并在其中设置UITableView的这些属性 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
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句