How can I add/remove cells in a table view more elegantly?

前端 未结 3 410
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 09:34

I think this is probably an X Y problem, so I\'ll give some background info first.

I am building an app that can show a \"form\". The user can fill in the form with

相关标签:
3条回答
  • 2020-12-20 10:04

    Because you don't have any data source. Also see logically: suppose you add a new row with insertRowsAtIndexPaths . So now you have 2 rows. However your numberOfRowsInSection always returning 1. so it will crash and vice versa for delete. Table view are supposed to work with a collection (NSArray, NSDictionary, NSSet etc.). For your help:

    Already they have nade a form as yours

    obj-c easy to understand how table view with data source work

    Adding, Updating, Deleting and Moving records using Swift.

    0 讨论(0)
  • 2020-12-20 10:05

    I found a solution!

    I basically keep an array of an array of cells for the table view to display:

    var cells: [[UITableViewCell]] = [[], [], []] // I have 3 sections, so 3 empty arrays
    

    And then I added these data source methods:

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return cells.count
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cells[section].count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return cells[indexPath.section][indexPath.row]
    }
    

    Now, I can add and remove cells super easily:

    func addCellToSection(section: Int, index: Int, cell: UITableViewCell) {
        cells[section].insert(cell, atIndex: index)
        tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: section)], withRowAnimation: .Left)
    }
    
    func removeCellFromSection(section: Int, index: Int) {
        cells[section].removeAtIndex(index)
        tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: section)], withRowAnimation: .Left)
    }
    

    With just two lines, I can add/remove cells with animation!

    0 讨论(0)
  • 2020-12-20 10:20

    You may use a TableView to create the form but you must design an appropriate dataSource to keep track of these data. Now the dataSource method must be modified to

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return yourDataSourceForSection.count
    }
    

    You may edit this dataSource when a delete operation is performed as follows

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            //Delete the row from the data source to ensure consistency
            self.array.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
    }
    

    You may use a model class as the dataSource.

    class NewCustomOperation: NSObject {
       var name: String?
       var rejectFloatingPoints: Bool?
       var inputs: AvailableInputs?
       var results: Results?
    }
    
    class AvailableInputs: NSObject {
       var name: [String]?
       var description: [String]?
    }
    
    class Results: NSObject {
       var name: [String]?
       var formula: [String]?
    }
    
    0 讨论(0)
提交回复
热议问题