zoukankan      html  css  js  c++  java
  • 在 UIViewController 中手动增加 TableView 出现 Type 'SomeViewController' does not confirm to protocol 'UITableViewDataSource' 问题的解决办法

    许多时候我们都有在普通的继承自 UIViewController 的控制器中使用 TableView 的需求,这时候就需要当前控制器类继承 UITableViewDelegate 和 UITableViewDataSource,然后再初始化:

    @IBOutlet weak var firstTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        firstTableView.delegate = self
        firstTableView.dataSource = self
    }
    

    这时候,firstTableView.dataSource = self 这一行会报错:

    Type 'SomeViewController' does not confirm to protocol 'UITableViewDataSource'
    

    解决方案如下:

    在该类中新增如下方法:

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 3
        }
        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("UserCenter", forIndexPath: indexPath) as UITableViewCell
            
            cell.textLabel.text = indexPath.row.description
            
            return cell
        }
    

    最终运行结果如下:

    Image

     
     
  • 相关阅读:
    MINA的session.close
    Maven构建灵活配置文件
    函数的凹凸性
    幂函数习题
    2017全国卷1文科第9题高考真题的解法
    指数函数习题
    三角形的四心的向量表示
    进退中体会数学运算和数学策略
    函数f(x+1)和f(x-1)的奇偶性
    函数的奇偶性周期性习题
  • 原文地址:https://www.cnblogs.com/Cheetah-yang/p/4669911.html
Copyright © 2011-2022 走看看