Type mytableview does not confirm to portocol UITableViewDataResource

继承UITableViewDataSource报上面这个总是,是重写协议时写错了

override func numberOfRowsInSection(section: Int) -> Int {
        return 3
    }

应该写下面这个

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

就正确了。

代码如下

//
//  MyTableView.swift
//  L02TableView
//
//  Created by Myron on 16/11/13.
//  Copyright © 2016年 Myron. All rights reserved.
//

import UIKit

class MyTableView: UITableView,UITableViewDataSource{

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.dataSource = self;
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
//    override func 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("cell")
        
        let label = cell?.viewWithTag(1) as! UILabel
        
        label.text = "Hello Table View"
        
        return cell!
        
    }
    

}

原文地址:https://www.cnblogs.com/ycclmy/p/6059339.html