N
N
Nikita2019-03-03 18:20:26
iOS
Nikita, 2019-03-03 18:20:26

How to pass data to a custom UITableViewCell before it is built?

I need to pass data to a cell when it is created in the tableView.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? LessonTableViewCell
       //  Внутри класса LessonTableViewCell ячейка заполняет саму себя переданными ей данными
      /*код*/
}

How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
doublench21, 2019-03-03
@snitron

An example from my project. ( I don’t provide the code in text, a terrible editor. )
In the delegate:
5c7bf8b3c0d26725364424.png

internal override func collectionView(
  _ collectionView: UICollectionView,
    cellForItemAt indexPath: IndexPath
  ) -> UICollectionViewCell
  {
    let cell = collectionView.dequeueReusableCell(
      withReuseIdentifier: CategoriesCollectionViewCell.reuseIdentifier,
      for: indexPath
    ) as! CategoriesCollectionViewCell

    let randomIndex = indexPath.item % CategoriesPlaceholder.images.count

    cell.configure(
      withImage: UIImage(named: CategoriesPlaceholder.images[randomIndex])!,
      andTitle: CategoriesPlaceholder.titles[randomIndex]
    )

    return cell
  }

Inside the cell:
5c7bf8c1617ce913362365.png
internal final class CategoriesCollectionViewCell: UICollectionViewCell {

 // =====================================
  // MARK: - Configure
  // =====================================

  internal func configure(withImage image: UIImage?, andTitle title: String) {
    illustrationCircle.image = image
    self.title.text = title
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question