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

以下是我定义的自己的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의 정의를 살펴보겠습니다

으아악

이 클래스는 UITableViewDataSource 프로토콜을 구현하는 데 필요하므로 UITableViewController를 상속하려면 이 두 메서드를 재정의해야 합니다.
둘째, 볼 수 있는 것은 전체 내용이 아니라 UITableViewController의 인터페이스뿐입니다.

伊谢尔伦

사용하는 UITableViewController 및 기타 시스템 컨트롤이 캡슐화되어 있습니다. 보이는 것은 .h 파일뿐이고 메서드의 구체적인 구현은 .m에 있으므로 이 두 메서드는 UITableView에 구현된

에 있습니다. 중
Peter_Zhu

이제 Swift가 오픈소스인데 UITableView.m 파일을 볼 수 있나요? ? ? 실제로, 그러나 다시 하위 클래스는 상위 클래스의 프로토콜 메서드를 재정의할 수도 있습니다! 그렇죠?

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!