简体   繁体   English

在didselectRowAt(Swift 3)时如何添加子行

[英]how can i add subrow when didselectRowAt ( Swift 3)

I want to add a sub row in row at index path with custom cell without using a library. 我想在不使用库的情况下使用自定义单元格在索引路径的行中添加子行。

When I select a cell, another cell appears below this cell, this operation is repeated for each cell Ps: the cell of which it will appear it is the same 当我选择一个单元格时,另一个单元格出现在该单元格的下方,对每个单元格Ps重复此操作:将要出现的单元格是相同的

I try this: 我尝试这样:

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()


    }

i have 1 section i have 6 rows 我有1节,我有6行

Instead of adding 1 to your array.count 而不是添加1到您的array.count

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

Add 1 more object to this array, because if not, you will get an error saying that you are trying to get more rows than your datasource array. 向该数组再添加1个对象,因为如果没有,则将收到一条错误消息,提示您试图获取比数据源数组更多的行。

So: 所以:

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()
}

should work. 应该管用。

Hi you are doing correct just insert the new element in the arrayEnum and then 嗨,您做得正确,只需在arrayEnum中插入新元素,然后

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

Here CurrentIndex in an index of element where you want to insert an element in array , it will automatically reflect in tableview 在这里,CurrentIndex在要在数组中插入元素的元素索引中,它将自动反映在tableview中

暂无
暂无

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

相关问题 Swift:如何将数据从“didSelectRowAt”传递到另一个 VC 而不显示它或转场? - Swift: How do I pass data from "didSelectRowAt" to another VC without presenting it or a segue? tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)将UITapGestureRecognizer添加到自定义UITableViewCell时不起作用 - tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) Doesn't work when I add a UITapGestureRecognizer to my custom UITableViewCell didSelectRowAt 未触发(Swift 5) - didSelectRowAt not triggering (Swift 5) 只有当我按下几秒钟时,Swift UITableView才会调用didSelectRowAt - Swift UITableView calls didSelectRowAt only if I press a few seconds 如何在Swift的紧凑型设备上的splitViewController中正确使用didSelectRowAt? - How to properly use didSelectRowAt in splitViewController on compact device in swift? 如何在表结构(数组)中包含URL并在didSelectRowAt中调用它们? - How can I include URLs in a Table Struct (Array) and call them in didSelectRowAt? 我如何操作主表,以便在 Swift 中使用 splitview 时可以向其添加日历 - How can i manipulate an master table so i can add an calender to it when using splitview in Swift 如何在Swift中为图像添加贴纸? - How can I add stickers to an image in Swift? 如何快速添加位置图标? - How can i add location icon in swift? 如何快速在键盘上添加文本字段? - How can I add textfield on keyboard in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM