如何更优雅地在表格视图中添加/删除单元格? [英] How can I add/remove cells in a table view more elegantly?
问题描述
我认为这可能是X Y问题,所以我先给出一些背景信息。
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 some stuff and create some custom things.
我认为最合适的事情是使用表格视图。我可以在每个表格单元格中显示需要填写的所有文本框。
I think the most suitable thing to do is to use a table view for the form. And I can display all the text boxes that need to be fill in, in each of the table cells.
这是截图:
添加新输入按钮将在底部插入新单元格被窃听。如果您向左滑动其中一个输入,则会出现删除按钮。您可以使用它来删除输入。
The "Add New Input" button will insert a new cell on the bottom when it is tapped. And if you swipe one of the inputs to the left, you get a "Delete" button. You can use that to delete the input.
如您所见,此表视图需要添加和删除行。
As you can see, this table view needs to add and delete rows.
最初,我使用的是名为TableViewModel的cocoapod,这使得这非常容易。但后来我在图书馆找到了一个非常严重的错误,所以我不想再使用它。
Originally, I was using a cocoapod called "TableViewModel" which makes this super easy. But then I found a really severe bug in the library so I don't want to use it anymore.
我尝试使用表格视图的 deleteRowsAtIndexPaths
和 insertRowsAtIndexPaths
方法。但如果我这样做:
I tried using the table view's deleteRowsAtIndexPaths
and insertRowsAtIndexPaths
methods. But if I do it like this:
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)
}
这将导致我删除一行后表格视图与数据源不一致的异常。这意味着我必须有代码来处理这种不一致。这肯定会让我的代码更难阅读。
It will result in an exception saying that the table view is inconsistent with the data source after I deleted one row. This means I have to have code to handle this inconsistency. This will definitely make my code harder to read.
插入方法也是如此。
另外,我需要跟踪多少行每个部分都有。而且我的代码变得非常混乱而且无法维护。
Also, I need to keep track of how many rows there are in each section. And my code just becomes really messy and unmaintainable with all that.
我还搜索了其他库,但它们都非常奇怪,并不像tableViewModel那么简单。他们似乎都要求我为表视图创建一个模型。在我的情况下,我不明白该怎么做。我只想显示一堆文本字段!
I have also searched for other libraries but they are all really weird and not as straightforward as "tableViewModel". They all seem to require me to create a model for the table view. I don't understand how to do that in my case. I just want to display a bunch of text fields!
如何更优雅地插入或删除行?我想我要么写一个表视图的扩展名
,要么学习为我的表视图编写一个模型。但我无法做到这两种方法。
How can I insert or delete rows more elegantly? I think I either need to write an extension
of the table view or learn to write a model for my table view. But I am able to do neither of these methods.
推荐答案
我找到了解决方案!
我基本上保留了一个单元格数组的数组,供表格视图显示:
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!
这篇关于如何更优雅地在表格视图中添加/删除单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!