Размер UITableViewCell не изменяется - PullRequest
1 голос
/ 19 апреля 2020

Я не могу понять, почему ячейки не меняются в соответствии с заданным файлом .xib

Это мой контроллер таблицы

import Foundation
import UIKit

class RecipeTableView: UIViewController {
    let cellIdentifier = "RecipeTableViewCell"

    @IBOutlet weak var recipeTableView: UITableView!
    private let localDatabaseManager: LocalDatabaseManager = LocalDatabaseManager.shared
    private var recipes = [Recipe]()

    override func viewDidLoad() {
        super.viewDidLoad()

        recipeTableView.dataSource = self
        recipeTableView.delegate = self

        //recipeTableView.rowHeight = UITableView.automaticDimension
        //recipeTableView.estimatedRowHeight = UITableView.automaticDimension

        self.recipeTableView.register(UINib(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier: cellIdentifier)

        localDatabaseManager.loadRecipes { [weak self] (recipes) in
            guard let recipes = recipes else {
                return
            }

            self?.recipes = recipes
            DispatchQueue.main.async {
                self?.recipeTableView.reloadData()
            }
        }
    }

//    override func viewWillAppear(_ animated: Bool) {
//        recipeTableView.estimatedRowHeight = 256
//        recipeTableView.rowHeight = UITableView.automaticDimension
//    }
}

extension RecipeTableView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        recipes.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? RecipeTableViewCell else {
            return UITableViewCell()
        }

        let recipe = recipes[indexPath.row]
        cell.configure(with: recipe)

        //cell.layer.cornerRadius = 32
        //cell.layer.masksToBounds = true

        return cell
    }
}

extension RecipeTableView: UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
}

И файл моей ячейки swift

import Foundation
import UIKit
import Kingfisher
import Cosmos

class RecipeTableViewCell: UITableViewCell {

    @IBOutlet weak var recipeNameLabel: UILabel!
    @IBOutlet weak var recipeDescriptionLabel: UILabel!
    @IBOutlet weak var recipeImageView: UIImageView!
    @IBOutlet weak var recipeCosmosView: CosmosView!

    override func prepareForReuse() {
        super.prepareForReuse()

        recipeNameLabel.text = nil
        recipeDescriptionLabel.text = nil
        recipeImageView.image = nil
    }

    func configure(with recipe: Recipe) {
        recipeNameLabel?.text = recipe.name
        recipeDescriptionLabel?.text = recipe.description

        //let imageBytes = recipe.imageData
        //let imageData = NSData(bytes: imageBytes, length: imageBytes.count)
        //let image = UIImage(data: imageData as Data)
        //recipeImageView?.image = image

        let imageUrl = URL(string: recipe.imageData)
        recipeImageView?.kf.setImage(with: imageUrl)

        recipeCosmosView.settings.fillMode = .precise
        recipeCosmosView.rating = recipe.rating
    }
}

вот как выглядит моя пользовательская ячейка

вот как эти ячейки отображаются в приложении

Я уже нашел похожие вопросы , но везде одинаковый ответ. Нужно добавить следующие строки. Поэтому я попытался.

override func viewWillAppear(_ animated: Bool) {
        recipeTableView.estimatedRowHeight = 256
        recipeTableView.rowHeight = UITableView.automaticDimension
}

Но это не сработало

1 Ответ

0 голосов
/ 19 апреля 2020

Я думаю, вам нужно вызвать другой метод tableView для установки высоты для каждой ячейки в соответствии с его содержимым

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // add estimated height here ....
     //...
    return indexPath.row * 20
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...