ios - 在委托(delegate)回调中包装手动 UItableViewCell 取消选择

标签 ios uitableview

我试图向我的同事解释,当他以编程方式执行此操作时:

[self.tableView deselectRowAtIndexPath:indexPath animated:NO];

他还应该将其“包装”在适当的委托(delegate)回调中。原因是在这种特殊情况下(取消选择不是交互式的,而是程序性的), TableView Controller 不会触发这些回调,因为它不知道取消选择发生时会发生。

这是 UITableView 类文档的一个片段:

Discussion

  • deselectRowAtIndexPath:animated:

Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor does it send UITableViewSelectionDidChangeNotification notifications to observers.

所以正确的实现应该是这样的:

[self tableview:self.tableView willDeselectRowAtIndexPath:indexPath];

[self.tableView deselectRowAtIndexPath:indexPath animated:NO];

[self tableview:self.tableView didDeselectRowAtIndexPath:indexPath];

如果未触发委托(delegate)回调,则公共(public)接口(interface)中的 promise 将被破坏,并且依赖于这些回调的功能将被破坏。他说目前没有依赖于这些回调的功能,所以这是不成熟的优化。在我看来,它不是,因为目标不是违背 promise 。

我说得对吗?

最佳答案

我认为你是对的,因为一条非常明智且重要的规则:

Principle of least astonishment

The Principle of Least Astonishment states that the result of performing some operation should be obvious, consistent, and predictable, based upon the name of the operation and other clues.

如果项目中的新人将来必须实现一些新功能,当他的 UITableView 取消选择自身但不调用委托(delegate)方法时,他会惊讶 ,因为那是明显、一致且可预测的行为。这是您所说的 promise

这是打破最小惊讶规则的一个很好的例子。

现在假设您在一个项目中有 100 个方法/对象,这些方法/对象是不可预测的。恐怖,对吧?这就是为什么遵循这个简单的原则非常重要。

关于ios - 在委托(delegate)回调中包装手动 UItableViewCell 取消选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28879671/

相关文章:

iphone - UITableViewCell 中的 UIButton

objective-c - 每个按钮都有不同 View 的 UITableView

iphone - 越狱的iOS : how resume(in foreground) from background

ios - AFNetworking 请求失败 : unacceptable content-type: text/html

ios - 依赖于不同队列上的另一个操作的 NSOperation 不会启动

iphone - 将 Max IndexPath 设置为高于 10?

uitableview - TableView :cellForRowAtIndexPath: in iOS Today Widget with UITableViewController called once

ios - 获取具有复选标记附件的单元格

ios - 如何在不禁用突出显示的情况下禁用uitable中的触摸行

android - Unity 和 2d 应用程序