【问题标题】:how can i add subrow when didselectRowAt ( Swift 3)didselectRowAt(Swift 3)时如何添加子行
【发布时间】:2017-08-06 13:33:00
【问题描述】:

我想在不使用库的情况下使用自定义单元格在索引路径的行中添加一个子行。

当我选择一个单元格时,另一个单元格出现在这个单元格下方,这个操作对每个单元格重复 Ps:它会出现的单元格是一样的

我试试这个:

func numberOfSections(in tableView: UITableView) -> Int {
        return 1

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {            
        return arrayEnum.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       var CellTB: UITableViewCell!

            let cell = tableView.dequeueReusableCell(withIdentifier: "enumCell", for: indexPath)as! UserFlagTableViewCell
            cell.UserflagLabel.text = arrayEnum[indexPath.row].rawValue
            CellTB = cell


//           let cell1 = tableView.dequeueReusableCell(withIdentifier: "freeTextCell", for: indexPath)as! FreeTextTableViewCell
//            cell1.sendButton.addTarget(self, action: Selector("sendUserflagEvent:"), for: .touchUpInside)
//            CellTB = cell1


              return CellTB

    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


        self.tableView.insertRows(at: [IndexPath(row: UserFlagType.userFlagTypes.count-1, section: 0)], with: .automatic)



        self.tableView.beginUpdates()
        self.tableView.endUpdates()


    }

我有 1 个部分 我有 6 行

【问题讨论】:

    标签: ios uitableview swift3 expand


    【解决方案1】:

    而不是将 1 添加到您的 array.count

    self.tableView.insertRows(at: [IndexPath(row: array.count+1, section: 0)], with: .automatic)
    

    再向这个数组添加 1 个对象,否则,您将收到一条错误消息,指出您正在尝试获取比数据源数组更多的行。

    所以:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //Add a row here to your array
    self.tableView.insertRows(at: [IndexPath(row: array.count - 1, section: 0)], with: .automatic)
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
    }
    

    应该可以。

    【讨论】:

    • 不,它对我不起作用,我如何显示新单元格??原因:'尝试将第 6 行插入第 0 节,但更新后第 0 节只有 6 行' libc++abi.dylib: 以未捕获的 NSException 类型异常终止
    • IndexPath 从 0 开始,你正在做 array.count,从 1 开始。所以......你必须做 array.count - 1。我会编辑答案,sry跨度>
    • 我有一个枚举列表(6 个枚举),我在表格视图中创建它,我希望当我选择一个单元格时出现一个新的自定义单元格(我如何有一个按钮和要发送的文本)。我不知道怎么做。我不能添加到可能枚举数组
    【解决方案2】:

    您好,您只是在 arrayEnum 中插入新元素,然后

    tableView.beginUpdates() tableView.insertRows(at: [IndexPath(row:CurrentIndex , section: 0)], with: .automatic) tableView.endUpdates()

    这里 CurrentIndex 在你想要在数组中插入元素的元素索引中,它会自动反映在 tableview 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多