如何更优雅地在表格视图中添加/删除单元格?

时间:2016-05-17 14:10:45

标签: ios swift uitableview model

我认为这可能是X Y问题,所以我先给出一些背景信息。

我正在构建一个可以显示“表单”的应用程序。用户可以用一些东西填写表单并创建一些自定义的东西。

我认为最合适的方法是使用表格视图。我可以在每个表格单元格中显示需要填写的所有文本框。

以下是截图:

enter image description here

“添加新输入”按钮将在点击时在底部插入新单元格。如果您向左滑动其中一个输入,则会出现“删除”按钮。您可以使用它来删除输入。

如您所见,此表视图需要添加和删除行。

最初,我使用的是名为“TableViewModel”的cocoapod,这让这非常容易。但后来我在库中找到a really severe bug,所以我不想再使用它了。

我尝试使用表格视图的deleteRowsAtIndexPathsinsertRowsAtIndexPaths方法。但如果我这样做:

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或者学习为我的表视图编写一个模型。但我无法做到这两种方法。

3 个答案:

答案 0 :(得分:0)

因为你没有data source。另请参阅logically:假设您使用insertRowsAtIndexPaths添加新行。所以现在你有2 rows。但是你的numberOfRowsInSection总是返回1.所以它会崩溃,反之亦然。表视图应该与collectionNSArrayNSDictionaryNSSet等一起使用。 求助:

Already they have nade a form as yours

obj-c easy to understand how table view with data source work

Adding, Updating, Deleting and Moving records using Swift.

答案 1 :(得分:0)

您可以使用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]?
}

答案 2 :(得分:0)

我找到了解决方案!

我基本上保留了一个用于显示表视图的单元格数组:

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

只有两行,我可以用动画添加/删除单元格!

相关问题