Swift 实现UITableView报错, does not conform to protocol 'UITableViewDataSource'
来源:互联网 发布:卖家网wish数据 编辑:程序博客网 时间:2024/06/05 04:51
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)
}
}
1 0
- Swift 实现UITableView报错, does not conform to protocol 'UITableViewDataSource'
- Swift开发教程--实现UITableView报错does not conform to protocol 'UITableViewDataSource‘
- Swift 实现UITableView报错, does not conform to protocol 'UITableViewDataSource'
- Swift报错type ''does not conform to protocol 'UITableViewDataSource'
- swift:does not conform to protocol解决方法
- Xcode 报错: Type '**Controller' does not conform to '**Delegate
- Swift Compiler Error Type 'int' does not conform to protocol 'Boolean Type'
- Swift UITableView遵从UITableViewDataSource报错问题
- type 'CFString!' does not conform to protocol 'NSCopying'
- Type 'ViewController' does not conform to protocol 'xxxdelegate'
- Swift3-Type 'String' does not conform to protocol 'Sequence'
- Xcode 报错: value of type InerfaceController does not conform to HKWorkoutSessionDelegate in assignmen
- type 'simple Class' does not conform to protocol 'Example Protocol'错误
- 编译ImageIO库type 'CFString!' does not conform to protocol 'NSCopying'
- 554 Message does not conform to standards
- Certificates does not conform to algorithm constraints
- Certificates does not conform to algorithm constraints
- swift错误: Type "xxx" does not confirm to protocol "BooleanType"
- DIV+CSS网页布局对搜索引擎优化SEO的影响
- java中静态代码块的用法 static用法详解
- asp开发之mysql数据库的两种使用方法
- 2.3 键——超键、备选键、主键、引用完整性约束
- password密码输入框禁止copy cut paste,jQuery快速搞定
- Swift 实现UITableView报错, does not conform to protocol 'UITableViewDataSource'
- 火车头v8.5使用示例 采集手机号码
- LeetCode OJ 之 Set Matrix Zeroes (矩阵的置0)
- 利用sql server事件探查器排除与性能相关的问题
- Ubuntu-更新源
- python学习笔记(一)
- 查询Sql Server锁
- STM32 不同型号配置 MDK 工程
- 最大流算法 SAP+GAP