ios - 请问关于swift中UITableViewController数据源的问题
PHP中文网
PHP中文网 2017-04-17 17:35:03
0
3
249

以下是我定义的自己的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个方式是在哪里实现的呢?

PHP中文网
PHP中文网

认证高级PHP讲师

全部回覆(3)
Peter_Zhu

首先第一個問題,我們來看看UITableViewController的定義

public class UITableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource

這個類別已經被要求實現UITableViewDataSource協議,所以繼承UITableViewController必須重寫這兩個方法
其次你所能看到的只是UITableViewController的接口,而不是其全部內容

伊谢尔伦

你用的UITableViewController 等等系統的控件,都是已經封裝好的,你所看到的只有.h 文件,而對方法的具體實現是在.m 裡,所以,這兩個方法是在UITableView. m 裡實現的

Peter_Zhu

現在說swift開源,那UITableView.m檔案能不能看到呢? ? ?確實是,不過話又說回來了,子類別也可以複寫父類別中的協議方法嘍!這樣說對不?

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!