在 uitableviewcell 中加载 tableview swift

load tableview inside uitableviewcell swift

您好,我正在尝试在 UITableViewCell 中添加 UITableview。我已将外部 tableview 单元格连接到视图控制器,将内部 tableview(单元格内部)连接到自定义单元格 class 并执行以下代码

//cellforrow of the outer tableview:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier("HistoryCell")! as! OrderHistoryTableViewCell
    print(ordersArray[indexPath.row].valueForKey("items"))
    cell.lbl_orderStatus.text = ordersArray[indexPath.row].valueForKey("status") as? String
    cell.lbl_time.text = ordersArray[indexPath.row].valueForKey("timestamp") as? String
    let vc = OrderHistoryTableViewCell() // Custom cell class
    vc.tableview_reload(ordersArray[indexPath.row]as! NSMutableDictionary) //pass value to the tableview inside the cell
    return cell
}

//code in the custom cell class

func tableview_reload(dict : NSMutableDictionary){
    orderItemsarray.removeAllObjects()
    let itemsDict = dict.valueForKey("items") as! NSMutableDictionary
    for (_,value) in itemsDict
    {
        let tempDict = value as! NSMutableDictionary
        orderItemsarray.addObject(tempDict)
    }
    self.tbl_Items.reloadData() // fatal error: unexpectedly found nil while unwrapping an optional value
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return self.orderItemsarray.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    var cell : UITableViewCell!
    cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell");
    cell.textLabel?.text = "test"
    return cell
}

传递数据,调用自定义单元格class中的函数tableview_reload。但是当我尝试在自定义单元格中重新加载表格视图时 class 致命错误:

unexpectedly found nil while unwrapping a optional value occurs.

我已经检查了插座连接,它已连接到自定义电池 class。请指教

无需使用 OrderHistoryTableViewCell() 创建新实例,您需要使用使用 dequeueReusableCellWithIdentifier 创建的重用单元格,因此删除行 let vc = OrderHistoryTableViewCell() 并调用 tableview_reload 方法cell

cell.tableview_reload(ordersArray[indexPath.row]as! NSMutableDictionary)
return cell