Ios viewcontroller中的多个uitableview出现错误

Ios viewcontroller中的多个uitableview出现错误,ios,swift,uitableview,Ios,Swift,Uitableview,我对管理viewcontroller中两个uitableview的以下代码有问题。在“directionTableView”的模态控制器中插入数据时,给出以下错误: 线程1:信号SIGABRT '无法将类型为'UITableViewCell'(0x1059e7560)的值强制转换为'FoodTime.DirectionRecipeTableViewCell'(0x101388bf0)。2018-05-23 21:50:12.160281+0200 FoodTime[4577:360265]无法将

我对管理viewcontroller中两个uitableview的以下代码有问题。在“directionTableView”的模态控制器中插入数据时,给出以下错误:

线程1:信号SIGABRT '无法将类型为'UITableViewCell'(0x1059e7560)的值强制转换为'FoodTime.DirectionRecipeTableViewCell'(0x101388bf0)。2018-05-23 21:50:12.160281+0200 FoodTime[4577:360265]无法将类型为“UITableViewCell”(0x1059e7560)的值强制转换为“FoodTime.DirectionRecipeTableViewCell”(0x101388bf0)。”


远离问题的第一线

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
        }

}
还要确保将每个tableview都注册到相应的单元格中,这很容易出错

你申报这个

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
}

1.到底是哪一行导致了错误?2.错误的完整、有用、可读的部分是什么?通常,每个类有一个TableView是有益的。这并不是说一个视图不能有多个TableView,但是您可以将表视图的委托和数据源委托给单独的控制器。这可能会在将来为您节省很多麻烦。删除tableView的
IBOutlets
,然后从情节提要或XIB@maddy:我已编辑代码并在错误行中插入注释。这是完整的错误消息:“无法将“UITableViewCell”(0x1059e7560)类型的值强制转换为“FoodTime.DirectionRecipeTableViewCell”(0x101388bf0)。2018-05-23 21:50:12.160281+0200 FoodTime[4577:360265]无法将类型为“UITableViewCell”(0x1059e7560)的值强制转换为“FoodTime.DirectionRecipeTableViewCell”(0x101388bf0)。“请确保已在InterfaceBuilder中将单元格的自定义类设置为
DirectionRecipeTableViewCell
。这不会解决导致崩溃的问题。”。它修复了其他问题并添加了一个新的问题。施胡汗:谢谢你的支持。我已经解决了这个问题……:)是的,答案末尾的注释将修复崩溃的基本原因。这不会修复崩溃。他应该确保具有
newDirectionCell
的单元格是
DirectionRecipeTableViewCell
,其他单元格也是如此one@AbdelahadDarwish:是的,你说得对!我失去了与班级的链接。。。太多了!
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
}