2016-11-03 30 views
0

我有一個tableview根據數組中的項目數(exerciseTableCells)加載部分的數量。然後根據該數組中項目的數量(ExercisesSetsTableCells)爲每個部分加載行數。兩個數組下面:如何從FooterInSection中的按鈕向UITableView中的特定部分添加一行

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"] 

我在我想僅將一行添加到表中的那個部分中的每個部分(或取決於部分中的按鈕陣列的該索引的頁腳的按鈕是)

我的表看起來像這樣,

enter image description here

+0

你可以瞭解目標C代碼? – KKRocks

+0

您首先需要更新數據源,即數組,然後將該單元格插入所需的部分。 –

回答

1

你需要得到indexpath使用使用以下點擊按鈕子視圖的層次:

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 

} 
1

您需要更新表和你在同一時間的數據源(這些陣列)。

類似的東西:

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

更新陣列和插入細胞,和beginUpdates()和endUpdates之間將它們包裝()。

其他選項只是爲了reloadData ...

+0

這很有用,但是如何確定按鈕在單擊時位於exerciseSetsTabelCells中哪個數組元素要附加到哪個部分? – MHDev

相關問題