首页 > 请问关于swift中UITableViewController数据源的问题

请问关于swift中UITableViewController数据源的问题

以下是我定义的自己的ViewController,继承了UITableViewController,教程指出我需要override重写tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> InttableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 两个方法,一个是定义行数,一个是获取可重用的cell。

import UIKit
class listViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0;
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("listItem", forIndexPath: indexPath)
        return cell
    }
}

我在Xcode中command点击进入继承的 UITableViewController 看了下:

@available(iOS 2.0, *)
public class UITableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    public init(style: UITableViewStyle)
    public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
    public init?(coder aDecoder: NSCoder)
    
    public var tableView: UITableView!
    @available(iOS 3.2, *)
    public var clearsSelectionOnViewWillAppear: Bool // defaults to YES. If YES, any selection is cleared in viewWillAppear:
    
    @available(iOS 6.0, *)
    public var refreshControl: UIRefreshControl?
}

没有看到有tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> InttableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 两个方法,我知道这两个方式是UITableViewDataSource协议中定义的,看下面,我又点击进入UITableViewDataSource协议中看了下:

public protocol UITableViewDataSource : NSObjectProtocol {
    
    @available(iOS 2.0, *)
    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    
    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
    
    @available(iOS 2.0, *)
    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    
    @available(iOS 2.0, *)
    optional public func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented
...以下省略...

我看到了这两个方法的定义,我不能理解的是:

  1. 教程要我用override重写tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> InttableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 两个方法。既然是用override那么就应该是重写了父类的存在方法,但是在UITableViewController中我没有看到这2个方法的定义?

  2. UITableViewDataSource中有定义,但是UITableViewDataSource是个协议,那么就是说这2个方法是必须要实现类来实现的,所以我想问这2个方式是在哪里实现的呢?


首先第一个问题,我们看看UITableViewController的定义

public class UITableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource

这个类已经被要求实现UITableViewDataSource协议,所以继承UITableViewController必须重写这两个方法
其次你所能看到的只是UITableViewController的接口,而不是其全部内容


现在说swift开源,那么UITableView.m文件能不能看到呢???确实是,不过话又说回来了,子类也可以复写父类中的协议方法喽!这样说对不?


你用的 UITableViewController 等等系统的控件,都是已经封装好的,你所看到的只有.h 文件,而对方法的具体实现是在.m 里,所以,这两个方法是在 UITableView.m 里实现的

【热门文章】
【热门文章】