Утечки памяти при вызовах dequeueReusableCell (withIdentifier: for :) swift - PullRequest
0 голосов
/ 03 апреля 2019

Я должен показать пару разных клеток.Я позвонил tableView(_:cellForRowAt:) для этого, и в методе я использую два разных идентификатора для разных классов UITableViceCell

Вот простой код:

class SimpleView: UITableViewController {
...

let cellIdMain = "JournalMainCellID"
let cellIdExtra = "JournalMainSceneAddNewID"

...

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == journals.count {
        guard let cellAdding = tableView.dequeueReusableCell(withIdentifier: cellIdExtra, for: indexPath) as? JournalMainSceneAddNew else {
            fatalError("Cannot connect to the cell")
        }
        return cellAdding
    }

    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdMain, for: indexPath) as? JournalMainSceneCell else {
        fatalError("Cannot connect to the cell")
    }
    cell.numberOfCountriesLabel.text = "\(journals[indexPath.row].numberOFCountries)"
    return cell
}
}

Когда я пытался найтиУтечки памяти, которые я обнаружил:

enter image description here

Когда я нажимал на детали, я нашел:

enter image description here

Почему это произошло?Это выглядит довольно просто и понятно.

Обновлено: фотографии были обновлены.

Ответы [ 2 ]

0 голосов
/ 04 апреля 2019

Обновите ваш код следующим кодом.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == journals.count {

        let cellAdding: JournalMainSceneAddNew = {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdExtra) as? JournalMainSceneAddNew else {                
                return JournalMainSceneAddNew(style: UITableViewCellStyle.value1, reuseIdentifier: cellIdExtra)
            }
            return cell
        }()

        return cellAdding
    }

    let cell: JournalMainSceneCell = {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdMain) as? JournalMainSceneCell else {                
            return JournalMainSceneCell(style: UITableViewCellStyle.value1, reuseIdentifier: cellIdMain)
        }
        return cell
    }()

    cell.numberOfCountriesLabel.text = "\(journals[indexPath.row].numberOFCountries)"
    return cell
}
0 голосов
/ 03 апреля 2019

вы пишете, если условия, которые будут работать только в одном случае как indexpath.row, будут равны count, даже если он не будет работать, потому что после прохождения, если он выполнит блок кода, после if, что означает ваш блок ifотходы и почему вы используете cell.delegate ??

...