Reputation: 145
I have a tableview that loads the number of sections depending on the number of items in an array (exercisesTableCells). Then the number of rows is loaded for each section depending on the count of items in that array (exercisesSetsTableCells). The two arrays are below:
var exercisesSetsTableCells: [[String]] = [["1","2","3"],["1","2"],["1","2","3","4"],["1","2"],["1","2","3","4"]]
var exercisesTableCells: [String] = ["Bench","Squat","Pull Ups","Curls","Sit Ups"]
I have a button in the footer of each section that I want to add only a row to that section of the table (or that index of the array depending on the section the button is in)
My table looks like this,
Upvotes: 0
Views: 71
Reputation: 8322
You need to get indexpath using clicked button subview's hierarchy using following :
func clickAction(_ sender: AnyObject) {
let button = sender as? UIButton
let cell = button?.superview?.superview as? UITableViewCell
let indexPath = tblview.indexPath(for: cell!)
let newArray = exercisesSetsTableCells.object(at: (indexPath?.row)!) as! NSMutableArray
newArray.add("new data");
exercisesSetsTableCells .replaceObject(at: (indexPath?.row)!, with: newArray);
//reload your tableview here
}
Upvotes: 1
Reputation: 1843
You need to update the table and your datasource (Those arrays) at the same time.
Something like that:
tableView.beginUpdates()
exercisesTableCells.appendElement("new item")
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Automatic) // of course, change the indexpath to the correct one
tableView.endUpdates()
Update the array and insert the cells, and wrap them between beginUpdates() and endUpdates().
Other option is just to reloadData...
Upvotes: 1