Попробуйте this-
если вы используете 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
}
}
Пожалуйста, перейдите по этой ссылке, это будет полезно: https://www.raywenderlich.com/129059/self-sizing-table-view-cells –
Ваш вопрос и принятый ответ несовместимы. Прочитайте заголовок вопроса, который вы попросили, и посмотрите на ответ, который вы приняли, @Prisy –
, но его работа с динамической высотой tableview в swift –