没有< UITableViewDelegate,UITableViewDataSource>的UITableView;仍然有效! [英] UITableView without <UITableViewDelegate, UITableViewDataSource> still works!

查看:46
本文介绍了没有< UITableViewDelegate,UITableViewDataSource>的UITableView;仍然有效!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读到,每当使用 UITableView 时,都应使控制器类符合< UITableViewDelegate,UITableViewDataSource> 协议(如果您没有使用量身定制的 UITableViewController 类)。

I've read that whenever you're using a UITableView you should conform your controller class to the <UITableViewDelegate, UITableViewDataSource> protocols (if you're not using the tailormade UITableViewController class).

所以我的超类是 UIViewController ,但是我忘记在.h文件中添加< UITableViewDelegate,UITableViewDataSource> 声明。由于某种原因,该表仍然可以工作。

So my superclass is UIViewController but I forgot to add the <UITableViewDelegate, UITableViewDataSource> declaration in the .h file. For some reason the table works anyways. It gets its data from the class and clicking cells works as expected.

有人可以向我解释一下它为什么起作用吗?

Can somebody explain this to me -- why it's working?

推荐答案

如果您的实现中仍然有适当的方法,它将可以正常工作。当表视图需要调用这些方法时,它不会检查您的类是否已声明它符合UITableViewDelegate或UITableViewDataSource,而是会检查您的类是否已实现所需的方法。

If you still have the appropriate methods in your implementation it will work correctly. When the table view needs to call those methods it doesn't check that your class has stated it conforms to UITableViewDelegate or UITableViewDataSource, instead it checks that your class implements the required methods.

明确声明您遵守协议的目的是使编译器可以生成警告。如果您尝试将ViewController的实例分配给ID,但未在标头中声明,则会收到编译器警告,但是如果实现了所有必需的UITableViewDataSourceDelegate方法,它仍然可以工作。

The point of explicitly stating that you conform to a protocol is so that the compiler can generate warnings. If you try and assign an instance of your ViewController to an id but haven't stated it in your header you will get compiler warnings, but it will still work if all the required UITableViewDataSourceDelegate methods are implemented.

这篇关于没有&lt; UITableViewDelegate,UITableViewDataSource&gt;的UITableView;仍然有效!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆