简体   繁体   中英

how to reload data in tableview inside tableview controller cell correctly in swift 4?

I have a table view(named B) inside one of the tableview controller(named A) cells and I have one button in tableView A cell(named add) and in that cell I have tableview B and tableview B has a button(named Delete) and textfield for each cells so I want when user push add button table view B reload and have a new row and when user push delete button table view B reload and remove that row for understanding better I have a video that you can see the link of that here https://ufile.io/5x6y1

in this video, you can see what will happen and what I want to do

and here is the codes for delete and add button in the main table view class ( remember the add button in the table view A cell (the main table view cell and the delete button is in the table view B cell (in each cell in table view B that is inside main table view (table view A) cell) )

    var certificates = [String]()
    if self.pf.Licences.count != 0 {
               for i in 0...self.pf.Licences.count - 1 {
                    print(self.pf.Licences[i].title!)
                            self.certificates.append(self.pf.Licences[i].title!)
                        }
                    }
                    self.tableView.reloadData()
                }

 @IBAction func deleteCer(_ sender: UIButton) {

    print("delete")
    let buttonRow = sender.tag
    certificates.remove(at: buttonRow)
    DispatchQueue.main.async {
        self.tableView.reloadRows(at: [IndexPath.init(row: 0, section: 6)], with: .right)
    }
}

@IBAction func addingCertificate(_ sender: UIButton) {

    print("add")
    if certificates.count != 0 {
        if certificates[certificates.count - 1] != "" {
            certificates.append("")
            DispatchQueue.main.async {
                self.tableView.reloadRows(at: [IndexPath.init(row: 0, section: 6)], with: .bottom)
                }
            }
        }
        else {
           print(certificates)
           certificates.append("")
            print(certificates)

            DispatchQueue.main.async {
                self.tableView.reloadRows(at: [IndexPath.init(row: 0, section: 6)], with: .bottom)

        }
    }
}

In order for your additions and deletions to work you need to look into how these should be done on a UITableView at https://developer.apple.com/documentation/uikit/uitableview , under the section "Inserting, Deleting, and Moving Rows and Sections".

You will essentially need to use insertRows(at:with:) and deleteRows(at:with:) whilst making sure that your data source is updated.

If you have a controller of some sort for your UITableView B, you should probably be sending the insert and delete messages to that, not the parent UITableView A.

On a different note, where you use certificates[certificates.count - 1] you could just use certificates.last .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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