ios - View Controller 中的多个 uitableview 给出错误

标签 ios swift uitableview

我对以下在 View Controller 中管理两个 uitableview 的代码有疑问。当在“directionTableView”中的模态 Controller 中插入数据时,出现以下错误:

Thread 1: signal SIGABRT 'Could not cast value of type 'UITableViewCell' (0x1059e7560) to 'FoodTime.DirectionRecipeTableViewCell' (0x101388bf0). 2018-05-23 21:50:12.160281+0200 FoodTime[4577:360265] Could not cast value of type 'UITableViewCell' (0x1059e7560) to '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
}

最佳答案

远离问题第一行

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
        }

}

还要确保使用相应的单元格注册每个表格 View

关于ios - View Controller 中的多个 uitableview 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50496302/

相关文章:

ios - 如何在项目中添加自定义 iOS 框架的引用(而不是复制)

javascript - 错误 : 180: EXCEPTION thrown ('dev!' ): - and ERROR:> aqsrv> 70: Exception caught in (null) - Error -1

ios - 如何在一个类中使用不同的委托(delegate)和数据源方法实现两个 Tableview?

ios - 在模拟器上运行,同时拥有不在模拟器上显示类的 SDK(阻止我运行)

ios - Flurry 在 iPhone 5 上启动需要 4 秒

swift - 是否可以在 ScreenSaverView 中使用 SpriteKit

带有 SpriteKit 的 iOS 通用设备应用程序,如何为所有 View 缩放节点?

ios - 使用 Swift 和 UITableView 实现表头 View 的正确方式

ios - 点击单元格时 UILabel 向右移动

ios - 所有可本地化字符串的字母顺序