繁体   English   中英

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

[英]How can I add/remove cells in a table view more elegantly?

我认为这可能是一个XY问题,因此我将首先提供一些背景信息。

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

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

这是屏幕截图:

在此处输入图片说明

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

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

最初,我使用的是名为“ TableViewModel”的cocoapod,它使超级简单。 但是后来我在库中发现了一个非常严重的错误 ,所以我不想再使用它了。

我尝试使用表视图的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 ,要么需要学习为表视图编写模型。 但是我不能做这两种方法。

因为您没有任何data source logically也可以看到:假设您使用insertRowsAtIndexPaths添加了新行。 因此,现在您有2 rows 但是,您的numberOfRowsInSection始终返回1.,因此它将崩溃,反之亦然。 表视图应该与collectionNSArrayNSDictionaryNSSet等)一起使用。 您的帮助:

他们已经像你一样裸体了

obj-c易于理解,带有数据源的表视图如何工作

使用Swift添加,更新,删除和移动记录。

您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM