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

以下是我定义的自己的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讲师

reply all(3)
Peter_Zhu

First question, let’s look at the definition of UITableViewController

public class UITableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource

This class has been required to implement the UITableViewDataSource protocol, so inheriting UITableViewController must override these two methods
Secondly, all you can see is the interface of UITableViewController, not its entire content

伊谢尔伦

The UITableViewController and other system controls you use are all encapsulated. All you see are .h files, and the specific implementation of the methods is in .m, so these two methods are in UITableView. Implemented in m

Peter_Zhu

Now that Swift is open source, can the UITableView.m file be seen? ? ? Indeed, but then again, subclasses can also override the protocol methods in the parent class! Is that right?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!