问题描述
我认为这可能是一个 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!
如何更优雅地插入或删除行?我想我要么需要编写表视图的 extension
,要么学习为我的表视图编写模型.但是这两种方法我都做不到.
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!
这篇关于如何更优雅地添加/删除表格视图中的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!