N
N
Neckvik2019-06-22 16:16:38
Swift
Neckvik, 2019-06-22 16:16:38

How to catch dynamic row changes in a table in SWIFT?

5d0e28ad778c2207278864.pngThe 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

2 answer(s)
D
doublench21, 2019-06-23
@doublench21

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;
}

First, you don’t need to do every time what you can and should do once.
Secondly, you can always track size changes by simply redefining the corresponding property.
(If you have a cell defined in the storyboard, then the first two methods can be deleted and everything written in the method awakeFromNib)
I also wrote a function inside the predefined property printf. See how many times this function is called and compare with how many times your code will be called in
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

The code
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;
  }
}

PS I hope I understand you correctly.

B
briahas, 2019-06-24
@briahas

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 question

Ask a Question

731 491 924 answers to any question