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

 longning817 发布于 2022-10-30 15:28

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

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

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

    2022-10-31 21:50 回答
  • 首先第一个问题,我们看看UITableViewController的定义

    public class UITableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource

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

    2022-10-31 21:50 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有