Answer the question
In order to leave comments, you need to log in
How to catch dynamic row changes in a table in SWIFT?
The problem is this, my line size is determined automatically, depending on the screen. The problem is that when func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
loads the table rows, it doesn't give the dimensions correctly. I take the height of the line to make the picture round, and it takes the wrong size (for example, which was the default in the storyboard), it takes less than it will be, since automatic resizing has not yet occurred.
Because it will change to the correct size of the string only when drawing, that is, after I return the string from this method.
Accordingly, the picture will round off incorrectly for me (I round off the picture correctly, it’s just that the size is given to me incorrectly)
How can I find out what size the row of the table will be before I return this row from the method?
Or how you can catch the change in the size of the string through the label or through the string functions.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "chat") as! ChatsTableViewCell;
cell.imageUser.image = UIImage.convertBase64ToImage(imageString: (data?.list[indexPath.row].imageUser)!)
cell.imageUser.layer.cornerRadius = cell.frame.size.height / 2;
cell.imageChat.clipsToBounds = true;
return cell;
}
Answer the question
In order to leave comments, you need to log in
This code is absolutely terrible.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "chat") as! ChatsTableViewCell;
cell.imageUser.image = UIImage.convertBase64ToImage(imageString: (data?.list[indexPath.row].imageUser)!)
cell.imageUser.layer.cornerRadius = cell.frame.size.height / 2;
cell.imageUser.clipsToBounds = true;
return cell;
}
awakeFromNib
) printf
. See how many times this function is called and compare with how many times your code will be called intableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
import UIKit
final class Cell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
imageView?.clipsToBounds = true
imageView?.contentMode = .scaleAspectFit
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var frame: CGRect {
didSet {
guard frame.height != oldValue.height else { return }
imageView?.layer.cornerRadius = frame.height / 2.0
print(#function)
}
}
}
final class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(Cell.self, forCellReuseIdentifier: "Cell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 21;
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.imageView?.image = UIImage(named: "c-1-8")!
cell.textLabel?.text = "Ooops";
return cell;
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.width * 0.25;
}
}
It's not clear to me - why bind the radius of the rounding of the picture to the height of the cell if you need a round picture ??? Then - the height of the cell must be tied to the height of the picture. And you have everything turned around - the cart goes ahead of the horse.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question