UITableView 单元格 - 编辑?

UITableView 单元格 - 编辑?

问题描述:

是否可以使用 UITableView 来将值输入到数据库的字段中.

Is it possible to use a UITableView to be able to enter a value into a database's field.

例如,如果我有一个 UITableView 指向数据库中的一个字段,并且如果我想在数据库中输入一个新条目 - 点击 UITableView 单元格,然后允许键盘输入到最终结束的单元格中成为数据库中的新记录??

For example, if I was to have a UITableView pointing to a field within a database and if I wanted to enter a new entry into the database - tap on the UITableView Cell that would then allow keyboard input into the cell which ultimately end up being a new record in the database??

这是可能的,但如果有可能并不意味着你应该这样做.

你可能会问为什么?

好吧!您试图将数据从视图直接输入到数据库,这是一种非常糟糕的做法.它不好的原因有很多,主要是效率和安全原因.您应该考虑使用 MVC 模式.

Well! you are trying to input data from view directly to database, this is a very bad practice. There are many reason for it being bad, the major is efficiency and security reasons. You should consider using MVC pattern.

既然完全有可能,我将解释如何做到这一点,并以包含真实代码示例的链接结束.

Now since its completely possible, I will explain the idea on how to do it and conclude with links that will have real code examples.

tableView:cellForRowAtIndexPath:添加带有标记的 TextField(以在将来获取引用)并将其添加到单元格的 contentView 并将其隐藏.

In tableView:cellForRowAtIndexPath: add a TextField with tag (to get the reference back in future) and add it to contentView of the cell and have it hidden.

现在在 tableView:didSelectRowAtIndexPath: 中将单元格 editing 属性设为 YES.

Now in tableView:didSelectRowAtIndexPath: make the cells editing property to YES.

然后,在tableView:willBeginEditingRowAtIndexPath:使用 viewWithTag: 方法获取对 contentview 中文本字段的引用并隐藏 textLabela 并取消隐藏文本字段.

Then, in tableView:willBeginEditingRowAtIndexPath: get the reference to the textfield in contentview using viewWithTag: method and hide the textLabela and unhide the textfield.

在文本字段的委托中 textFieldDidEndEditing: 将单元格的编辑属性设置为 no(是的,您需要保留引用)取消隐藏文本标签并隐藏文本字段.

In textfield's delegate textFieldDidEndEditing: make cell's editing property as no (yea, you need to keep the reference) unhide the textlabel and hide textfield.

tableView:didEndEditingRowAtIndexPath: 中编写将更改提交到您的数据库的方法.

In tableView:didEndEditingRowAtIndexPath: write methods which will commit the changes to your db.

以下是可以为您提供代码示例的链接列表:

Below are list of links which will get you code examples:

在 UITableViewCell 中有一个 UITextField

在自定义 UITableViewCell 中访问 UITextField

iOS 数据库教程

没有满足您要求的示例,因为它的做事方式有点糟糕.

There are no examples for your requirement 'coz it bit bad way of doing things.