专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Swift

Swift 实现UITableView出错, does not conform to protocol 'UITableViewDataSource'

发布时间:2011-06-30 07:29:58 文章来源:www.iduyao.cn 采编人员:星星草
Swift 实现UITableView报错, does not conform to protocol 'UITableViewDataSource'

Swift语言中的UITableView中着实很坑爹,为什么呢,因为在遵循协议后经常会报这样的错误:does not conform to protocol 'UITableViewDataSource'。而且是第一次尝试的伙伴们经常会发现,我写的代码没有问题呀,该写的都写了,为什么还是报错呢,有的时候是xcode的问题,有的时候又是自己遵循的协议中有必需实现的方法而没有实现导致的。所以遇到这种问题,大家不妨跳进代理中去看看必须实现的方法都实现的没有。

下面是我写的一个小demo,大家可以看看。

import UIKit


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
    
    var dataArray: NSMutableArray?
    var tableView: UITableView?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = UIColor.orangeColor()
        self.navigationItem.title = "您好 Swift"
        
        self.dataArray = NSMutableArray()
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        self.dataArray!.addObject("您好 Swift")
        
        
        NSLog("%@", self.dataArray!)
        
        self.tableView = UITableView(frame: self.view.frame, style:UITableViewStyle.Plain)
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyTestCell")
        self.view.addSubview(self.tableView!)
        
        
    }
 
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return self.dataArray!.count
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
        
        var string :String = self.dataArray?.objectAtIndex(indexPath.row)as String
        cell.textLabel.text = string
        cell.textLabel.text = string
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        var controller = secondViewController()
        controller.title = "Swift 的自我介绍"
        controller.view.backgroundColor = UIColor(red: 10/255.0, green: 100/255.0, blue: 200/255.0, alpha: 1.0)
        self.navigationController?.pushViewController(controller, animated: true)
    }
    
   
    
}



友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: