2016-05-17 118 views
0

我认为这可能是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或学习为我的表视图编写模型。但是我能够完成这两种方法。

+0

因为您没有任何数据源。另请参阅逻辑:假设您使用insertRowsAtIndexPaths添加了一个新行。所以现在你有两排。然而你的numberOfRowsInSection总是返回1,所以它会崩溃,反之亦然。表视图应该与集合(NSArray,NSDictionary,NSSet等)一起工作。 – Alok

+0

事情是,当你添加或删除一行时,你总是说在该部分只有一行。您必须使用数组或字典来跟踪每行和每个部分中的数据(和哪些数据),并在'numberOfSectionsInTableView:'和'tableView:'numberOfRowsInSection:'中返回该值。 – Larme

+0

你的意思是我不应该使用表视图来做这种表单?那我能做些什么呢? @Alok – Sweeper

回答

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

只需两行,我就可以添加/删除带有动画的单元格!

0

因为您没有任何data source。另请参阅logically:假设您使用insertRowsAtIndexPaths添加了一个新行。所以现在你有2 rows。然而你的numberOfRowsInSection总是返回1.所以它会崩溃,反之亦然。表视图应与collectionNSArray,NSDictionary,NSSet等)一起使用。 对你有所帮助:

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.

0

你可以使用一个TableView创建形式,但你必须设计一个合适的数据源来跟踪这些数据。现在如下

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的数据源方法必须进行修改,以

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return yourDataSourceForSection.count 
} 

您可以编辑这个数据源当执行删除操作。

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]? 
} 
+0

是的,我知道,但如果我有一堆用户填写的文本框,而不是一堆要显示的数据,那么数据源会是什么? – Sweeper

+0

你有没有考虑过使用堆栈视图?也许很多工作,但他们会为你管理你的界面非常流畅。 – user3069232

+0

@ user3069232但我使用iOS8,但我不认为堆栈视图可用。 – Sweeper

相关问题