[英]How can I add/remove cells in a table view more elegantly?
我認為這可能是一個XY問題,因此我將首先提供一些背景信息。
我正在構建一個可以顯示“表單”的應用程序。 用戶可以在表格中填寫一些內容並創建一些自定義內容。
我認為最合適的方法是為表格使用表格視圖。 我可以在每個表格單元格中顯示所有需要填寫的文本框。
這是屏幕截圖:
輕按“添加新輸入”按鈕后,它將在底部插入一個新單元格。 並且,如果您向左滑動輸入的其中之一,則會出現“刪除”按鈕。 您可以使用它來刪除輸入。
如您所見,此表視圖需要添加和刪除行。
最初,我使用的是名為“ TableViewModel”的cocoapod,它使超級簡單。 但是后來我在庫中發現了一個非常嚴重的錯誤 ,所以我不想再使用它了。
我嘗試使用表視圖的deleteRowsAtIndexPaths
和insertRowsAtIndexPaths
方法。 但是,如果我這樣做:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Hello"
return cell
}
// I use motionEnded here because I don't want to add a button or anything just to test this
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade)
}
刪除一行后,將導致異常,表明表視圖與數據源不一致。 這意味着我必須具有處理這種不一致的代碼。 這肯定會使我的代碼更難閱讀。
插入方法也一樣。
另外,我需要跟蹤每個部分中有多少行。 而且我的代碼變得非常混亂,無法進行所有維護。
我還搜索了其他庫,但它們確實很奇怪,不像“ tableViewModel”那樣簡單。 它們似乎都要求我為表視圖創建模型。 我不了解該如何處理。 我只想顯示一堆文本字段!
如何更優雅地插入或刪除行? 我想我要么需要編寫表視圖的extension
,要么需要學習為表視圖編寫模型。 但是我不能做這兩種方法。
因為您沒有任何data source
。 logically
也可以看到:假設您使用insertRowsAtIndexPaths添加了新行。 因此,現在您有2 rows
。 但是,您的numberOfRowsInSection
始終返回1.,因此它將崩潰,反之亦然。 表視圖應該與collection
( NSArray
, NSDictionary
, NSSet
等)一起使用。 您的幫助:
您可以使用TableView
創建表單,但是必須設計適當的dataSource來跟蹤這些數據。 現在,必須將dataSource方法修改為
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourDataSourceForSection.count
}
您可以按如下所示執行刪除操作時編輯此dataSource
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)
}
}
您可以將模型類用作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]?
}
我找到了解決方案!
我基本上保留一個單元格數組以供表視圖顯示:
var cells: [[UITableViewCell]] = [[], [], []] // I have 3 sections, so 3 empty arrays
然后,我添加了以下數據源方法:
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]
}
現在,我可以輕松地添加和刪除單元格:
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)
}
只需兩行,我就可以添加/刪除帶有動畫的單元格!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.