简体   繁体   English

是否存在用于简化自定义UITableView单元格的委托方法或其他标准方法?

[英]Is there a delegate method or other standard approach for facilitating editing of custom UITableView cells?

This feels like a strange question to ask, but I've not managed to find an answer in any Apple documentation, online articles on the topic, or example code from various third-party solutions I'm working with (like Realm), so I'd appreciate it if someone experienced in iOS development could help me figure this out. 这听起来像是一个奇怪的问题,但是我没有设法在任何Apple文档,有关该主题的在线文章或我正在使用的各种第三方解决方案(例如Realm)的示例代码中找到答案,因此如果有iOS开发方面的经验的人可以帮助我解决这个问题,我将不胜感激。

In all UITableView tutorials, articles, and example code snippets in Apple's own documentation, the one and only showcased way of performing certain actions when a UITableViewCell's contents change is with the help of the following delegate method: 在Apple自己的文档中的所有UITableView教程,文章和示例代码段中,当UITableViewCell的内容更改时,唯一且唯一展示的执行某些操作的方法是借助以下委托方法:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)

However, it seems like this method only works for default UITableViewCells (rather than custom prototype cells with custom elements like UITextFields and UIImages) and relies on the use of the default delete/add buttons... unless I'm mistaken, which I very well could be. 但是,似乎此方法仅适用于默认的UITableViewCells(而不是具有自定义元素的自定义原型单元,如UITextFields和UIImages等自定义元素),并且依赖于使用默认的删除/添加按钮...除非我弄错了,我非常很好。

So my question is: can this method also be used to work with custom prototype cells that use additional elements like UITextFields? 所以我的问题是:此方法是否还可以用于使用其他元素(如UITextFields)的自定义原型单元?

Or is there perhaps some other method for that? 还是有其他方法可以解决?

Or is it, in fact, a matter of custom coding everything, eg a Did End On Exit IBAction for a custom text field in the cell? 还是实际上是对所有内容进行自定义编码的问题,例如,单元格中自定义文本字段的退出结束IBAction是否结束?

Or am I altogether barking up the wrong tree, and I need to be looking into some kind of notification service that I need to setup? 还是我完全吠叫了错误的树,并且需要研究需要设置的某种通知服务?

I just can't shake the feeling that I'm missing some small but extremely vital aspect of working with custom table cells, so any insight would be much appreciated. 我只是无法撼动这种感觉,因为我缺少使用自定义表格单元格的一些小而极其重要的方面,因此任何见解将不胜感激。

TL;DR: Need to know if there's a method or standard/common approach for detecting all possible changes to a custom table cell, ie when the text in a custom text field in the cell is edited and committed, when a cell is deleted, when a cell is added. TL; DR:需要知道是否存在用于检测对自定义表格单元格的所有可能更改的方法或标准/通用方法,例如,当编辑并提交该单元格的自定义文本字段中的文本时,何时删除该单元格,当添加一个单元格时。

The editingStyle delegate that you posted can be used for custom UITableViewCell and is mainly used for deletion. 您发布的editingStyle委托可用于自定义UITableViewCell ,主要用于删除。 But be that as it may, there is an amazing tutorial on the Ray Wenderlich site that shows you how to "fake" the same behaviour for your own needs in case you need to add new buttons and offer more functionality. 但是无论如何Ray Wenderlich网站上有一个很棒的教程 ,向您展示了如何根据自己的需要“伪造”相同的行为,以防您需要添加新按钮并提供更多功能。 You can also set custom views and setup various actions. 您还可以设置自定义视图并设置各种操作。 (Check out this post). (查看帖子)。

The only way to capture events like actions on UITextFields or UIButton or UITextView or any such thing is to set up appropriate delegate methods for the concerned event like textFieldDidBeginEditing or add a selector for the UIButton even if it is present inside a UITableViewCell . 捕获事件(如对UITextFieldsUIButtonUITextView或此类事件的唯一方法是为相关事件(如textFieldDidBeginEditing设置适当的delegate方法,或为UIButton添加selector (即使它存在于UITableViewCell You will have to manage identifying which particular cell was editted by setting tags of the UIView under question to be the indexPath.row or some other logic that you find. 您将必须通过将有问题的UIView标签设置为indexPath.row或找到的其他逻辑来管理识别哪个特定单元格被indexPath.row (Apart from using a UITableViewController which allows you create outlets directly or IBAction for a particular UIButton .) (除了使用UITableViewController之外,该UITableViewController允许您直接为特定的UIButton创建出口或IBAction 。)

Hope this answers your query! 希望这能回答您的查询!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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