Несколько uitableview в viewcontroller дают ошибку - PullRequest
0 голосов
/ 23 мая 2018

У меня проблема со следующим кодом, который управляет двумя uitableview в viewcontroller.При вставке данных в модальный контроллер в 'directionTableView' выдается следующая ошибка:

Поток 1: сигнал SIGABRT 'Невозможно привести значение типа' UITableViewCell '(0x1059e7560) в 'FoodTime.DirectionRecipeTableViewCell' (0x101388bf0).2018-05-23 21: 50: 12.160281 + 0200 FoodTime [4577: 360265] Не удалось привести значение типа «UITableViewCell» (0x1059e7560) к «FoodTime.DirectionRecipeTableViewCell» (0x101388bf0). '

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    var cell = UITableViewCell()
    if (tableView == self.ingredientTableView)
    {

        let cell = tableView.dequeueReusableCell(withIdentifier: "newIngredientCell", for: indexPath) as! IngredientRecipeTableViewCell

        let ingredientCell = ingredients[indexPath.row]
        cell.textLabel?.text = ingredientCell.titleIngredientRecipe
        cell.detailTextLabel?.text = ingredientCell.subtitleIngredientRecipe
    }
    else if (tableView == self.directionTableView)
    {
        //Thread 1: signal SIGABRT on next line
        let cell = tableView.dequeueReusableCell(withIdentifier: "newDirectionCell", for: indexPath) as! DirectionRecipeTableViewCell                 
        let directionCell = directions[indexPath.row]
        cell.textLabel?.text = directionCell.directionSection
        cell.detailTextLabel?.text = directionCell.directionText
    }
    return cell
}

Ответы [ 2 ]

0 голосов
/ 23 мая 2018

ошибка, я так просто

вы объявляете это

var cell = UITableViewCell()

, а затем создаете новый

    let cell = tableView.dequeueReusableCell(withIdentifier: "newIngredientCell", for: indexPath) as! IngredientRecipeTableViewCell

просто удалите let

Также

В конструкторе интерфейса убедитесь, что ячейка с newDirectionCell равна DirectionRecipeTableViewCell и такая же для другой ячейки

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    var cell = UITableViewCell()
    if (tableView == self.ingredientTableView)
    {

        cell = tableView.dequeueReusableCell(withIdentifier: "newIngredientCell", for: indexPath) as! IngredientRecipeTableViewCell

        let ingredientCell = ingredients[indexPath.row]
        cell.textLabel?.text = ingredientCell.titleIngredientRecipe
        cell.detailTextLabel?.text = ingredientCell.subtitleIngredientRecipe
    }
    else if (tableView == self.directionTableView)
    {
        //Thread 1: signal SIGABRT on next line
        cell = tableView.dequeueReusableCell(withIdentifier: "newDirectionCell", for: indexPath) as! DirectionRecipeTableViewCell                 
        let directionCell = directions[indexPath.row]
        cell.textLabel?.text = directionCell.directionSection
        cell.detailTextLabel?.text = directionCell.directionText
    }
    return cell
}
0 голосов
/ 23 мая 2018

Вдали от проблемы, первая строка

var cell = UITableViewCell()

- это фактически то, что возвращается ячейкам внутри операторов if, которые являются локальными переменными

Так что попробуйте это

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

        if (tableView == self.ingredientTableView)
        {

            let cell = tableView.dequeueReusableCell(withIdentifier: "newIngredientCell", for: indexPath) as! IngredientRecipeTableViewCell

            let ingredientCell = ingredients[indexPath.row]
            cell.textLabel?.text = ingredientCell.titleIngredientRecipe
            cell.detailTextLabel?.text = ingredientCell.subtitleIngredientRecipe

             return cell
        }
        else  
        {

            let cell = tableView.dequeueReusableCell(withIdentifier: "newDirectionCell", for: indexPath) as! DirectionRecipeTableViewCell //Thread 1: signal SIGABRT

            let directionCell = directions[indexPath.row]
            cell.textLabel?.text = directionCell.directionSection
            cell.detailTextLabel?.text = directionCell.directionText

            return cell
        }

}

Также убедитесь, что вы регистрируете каждый просмотр таблицы в соответствующей ячейке

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...