简体   繁体   English

Swift-UITableViewController中的单元格内的UITableView

[英]Swift - UITableView inside Cell from UITableViewController

I am trying to add a TableView inside my UITableViewCellController like this: 我试图像这样在UITableViewCellController添加一个TableView

在此处输入图片说明

To add content to the cells i'm doing this: 要将内容添加到单元格中,我正在这样做:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if (tableView == groupPicker) {
        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "")

        cell.textLabel?.text = "\(groups[indexPath.row]) - \(sets[indexPath.row]) Set\(ending(sets[indexPath.row]))"

        return cell
    }

    let cell = tableView.dequeueReusableCellWithIdentifier("cell\(indexPath.section)") as UITableViewCell

    return cell
}

groupPicker is my tableview inside the cell groupPicker是单元格内的tableview

I get this error: 我收到此错误:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) 致命错误:解开可选值(lldb)时意外发现nil

on this line: 在这条线上:

let cell = super.tableView.cellForRowAtIndexPath(indexPath)

Is there another way to do this, or am I doing something from? 还有另一种方法可以做到这一点,还是我从中做某件事?

(All my cells are static, I need the table view inside the cell to be dynamic) (我所有的单元格都是静态的,我需要单元格内部的表格视图是动态的)

Thanks in advance 提前致谢

UPDATED CODE: 更新的代码:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if (indexPath.section == 0) {
            var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "")
            cell.textLabel!.text = "Text"

            return cell
        }
        return tableView.dequeueReusableCellWithIdentifier("cell\(indexPath.section)") as UITableViewCell!
    }

I get this: (lldb) Nothing else 我得到这个:(lldb)

Firstly, you don't need to allocate the cell. 首先,您不需要分配单元。 All you need is to dequeue cells from your tableView. 您所需要做的就是从tableView中使单元出队。 Secondly, pass an empty string instead of nil in your cell identifier. 其次,在您的单元格标识符中传递一个空字符串而不是nil。 Finally and it's recommendation, separate the implementation of your cells in two different cell types. 最后,建议将单元的实现分为两种不同的单元类型。 The one that contains the tableView should conform on your TableView delegate and dataSource methods first. 包含tableView的表应首先符合TableView委托和dataSource方法。

Edit: 编辑:

You can limit the editing in your tableView based on a specific section using this delegate method 您可以使用此委托方法基于特定部分来限制tableView中的编辑

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
       editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

Does this maybe help? 这可能有帮助吗?

ios swift: How to have one static cell in a dynamic tableview? ios swift:如何在动态表格视图中有一个静态单元格?

I also think you need a unique identifier for each cell. 我还认为您需要每个单元格都有唯一的标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM