【问题标题】:Does UITableViewController equal to UITableViewDataSource, UITableViewDelegateUITableViewController 是否等于 UITableViewDataSource、UITableViewDelegate
【发布时间】:2013-02-22 09:48:29
【问题描述】:

我们能不能用UITableViewDataSourceUITableViewDelegate完全代替UITableViewController,比如这两行代码有区别吗?

@interface ShareViewController : UITableViewController
@interface ShareViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    这两行代码有很大的不同。 这个

    @interface ShareViewController : UITableViewController
    

    意味着您的 ShareViewController 继承自 UITableViewController,因此它提供了例如 UITableView 作为主视图。 这个

    @interface ShareViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
    

    意味着您的 ShareViewController 是一个经典的 UIViewController(继承自 UIViewController)并且只是实现了管理表格视图的协议。

    【讨论】:

      【解决方案2】:

      UITableViewController 最重要的是一个 UIView Controller

      它实现了UITableViewDataSourceUITableViewDelegate 协议,并有一个tableView 属性/出口(这是他唯一的.view)。

      一个 UITableView 是一个 UIView


      要让UIViewController 几乎可以完成UITableViewController 为您所做的所有事情,您必须实现这两个协议,将UITableView 拖到您的UIViewController 中,为其创建一个出口并设置您的@987654329 @ 作为您的tableView 出口的delegatedataSource

      您可以将UIRefreshControl 添加到UITableViewControllers tableView,只需单击 iOS6+ 的 StoryBoard 中的复选标记,您必须在 UITableView 内的 UITableViewController 内写更多的行来执行此操作

      【讨论】:

        【解决方案3】:
        @interface ShareViewController : UITableViewController
        

        以上行表示ShareViewController 继承自UITableViewController

        @interface ShareViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
        

        以上行表示ShareViewController 继承自UIViewController 并实现UITableViewDataSource, UITableViewDelegate 代表

        【讨论】:

          【解决方案4】:

          如果您检查UITableViewController 的引用,您会发现它符合UITableViewDataSourceUITableViewDelegate,但仅仅符合相同的协议并不能使类相等。 UITableViewController 是一个适合与UITableViews 一起使用的类,其中UIViewController 可以与任何UIView 一起使用。

          【讨论】:

            【解决方案5】:

            UITableViewController 是一个UIViewController 已经实现了UITableViewDelegate, UITableViewDataSource 协议,因此当你用它继承你的类时,你不需要再次在ViewController 中实现它们。就像:

            NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
            

            为了更清楚,UITableViewDelegate, UITableViewDataSourceprotocols,而 UITableViewControllerViewController

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-04-05
              • 1970-01-01
              • 2019-02-10
              • 2020-04-08
              • 2021-09-18
              相关资源
              最近更新 更多