ios - 在 uitableviewcell swift 中加载 tableview

标签 ios iphone swift uitableview

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

//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
}

数据传递完毕,调用自定义cell类中的func tableview_reload。但是当我尝试在自定义单元格类中重新加载 TableView 时出现 fatal error :

unexpectedly found nil while unwrapping a optional value occurs.

我检查了 socket 连接,它连接到自定义单元类。请指教

最佳答案

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

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

关于ios - 在 uitableviewcell swift 中加载 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41296832/

相关文章:

iphone - xcode 4.2,用于分发的代码签名

jquery - 通过 jquery 验证应用内购买 - 21002 错误

ios - 使用 UITapGestureRecognizer 检测 UITableViewCell 内 UILabel 的点击事件

ios - swift:键盘观察器不起作用

ios - 如何跟踪用户的位置足够长的时间来获取位置?

php - 查看 JSON 提要 Swift 3.0

iphone - 应用程序在调用 popViewController : error: alertView:didDismissWithButtonIndex: 时崩溃

iphone - 内存管理和性能

iphone - 如何在 Xcode 中显示/隐藏 View

ios - 如何在 Storyboard中更改 View Controller 的标题