Swift 实现UITableView报错, does not conform to protocol '
发布时间:2020-12-14 06:42:59 所属栏目:百科 来源:网络整理
导读:Swift语言中的UITableView中着实很坑爹,为什么呢,因为在遵循协议后经常会报这样的错误:does not conform to protocol 'UITableViewDataSource'。而且是第一次尝试的伙伴们经常会发现,我写的代码没有问题呀,该写的都写了,为什么还是报错呢,有的时候是x
Swift语言中的UITableView中着实很坑爹,为什么呢,因为在遵循协议后经常会报这样的错误:does not conform to protocol 'UITableViewDataSource'。而且是第一次尝试的伙伴们经常会发现,我写的代码没有问题呀,该写的都写了,为什么还是报错呢,有的时候是xcode的问题,有的时候又是自己遵循的协议中有必需实现的方法而没有实现导致的。所以遇到这种问题,大家不妨跳进代理中去看看必须实现的方法都实现的没有。 下面是我写的一个小demo,大家可以看看。 import UIKit
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) } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |