在 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
}
最终运行结果如下:

在 UIViewController 中手动增加 TableView 出现 Type 'SomeViewController' does not confirm to protocol 'UITableViewDataSource' 问题的解决办法的更多相关文章
- XP系统中IIS访问无法显示网页,目前访问网站的用户过多。终极解决办法
无法显示网页 目前访问网站的用户过多. -------------------------------------------------------------------------------- ...
- Spring中手动增加配置文件中占位符引用的变量
在项目中遇到一个这样的需求,项目的配置文件由外部传入,这时spring配置文件那些占位符变量该如何取值呢? 解决这个问题的做法有几种,我想到的大概有以下三种: 1.通过系统属性来实现,把外部传入的配置 ...
- Eclipse中利用JSP把mysql-connector-java-8.0.13.jar放到WebContent\WEB-INF\lib中连接MySQL数据库时Connection conn = DriverManager.getConnection(url,username,password)报错的解决办法
开发环境: 1.系统:windows 7/8/10均可 2.jdk:1.8.0_144 3.服务器:apache-tomcat-9.0.8 4.IDE:eclipse+jsp 0.网页代码如下: &l ...
- 关于ligerui 中 grid 表格的扩展搜索功能在远程数据加载时无法使用的解决办法
要想使用grid里的扩展搜索功能,除了要引用ligerui主要的js文件外,还必须引入下面的JS文件: 1.Source\demos\filter\ligerGrid.showFilter.js 2. ...
- Type Call requires API level 11 (current min is 8)解决办法
解决办法: 1:project-->clean.. 2:右键工程-->Android Tools-->clean lint markers 3:修改AndroidManifest.x ...
- 转:在eclipse中 使用7.0及以上手机进行测试时logcat不打印日志的解决办法
解决办法 替换ADT中的ddmlib.jar文件. 下载ADT对应的zip包,解压出ddmlib.jar文件 放到eclipse\configuration\org.eclipse.osgi\bund ...
- The type javax.http.HttpServletRequest cannot be resolved.It is indirectly 解决办法
原因:项目中缺少servlet-api.jar文件. 解决办法:将E:\tomcat\apache-tomcat-6.0.24\lib下的servlet-api.jar拷贝到项目中,然后编译即可.(根 ...
- 在Closing事件中,将e.Cancle设置成true,则Windows无法关机和重启系统的解决办法
最近在设计一个WinForm程序的时候遇到一个bug,就是From1窗体的关闭事件中设置了e.Cancle设置成true,导致系统无法关机重启,windows7 和windows xp都是这样. 我这 ...
- jquery中,使用append增加新元素时,新增元素的绑定监听事件失效的解决办法
$("outerSelector").on("eventType","innerSelector",function(){}); 举例:如果 ...
随机推荐
- css3-calc用法
css3--calc()使用 css3新增的一个功能,可以计算元素的长度 例如说:一个百分百布局中,分左右两侧,中间需要一个分隔空间,使用padding或者margin则会超出100%,这时候使用ca ...
- java中两字符串比较--compareTo方法
java.lang.String.compareTo() 方法比较两个字符串的字典,比较是基于字符串中的每个字符的Unicode值 String n1 = "1"; String ...
- php获取accesstoken和二维码的实现方法
class WeChat{ private $_appid; private $_appsecret; private $_token; public function __construct($_a ...
- undefined reference to '__android_log_print'解决方案
1:在源程序中添加头文件 #include <cutils/log.h> 2:在Android.mk中添加 LOCAL_SHARED_LIBRARIES := \ libutils \ l ...
- Android wifi 从连接态自动断开的解决办法(dhcp导致)【转】
本文转载自:http://blog.csdn.net/DKBDKBDKB/article/details/38490201 对wifi部分的代码流程已经看了段时间,前两天终于解决了工作中遇到的一个wi ...
- sdut oj 2372 Annoying painting tool (【暴力枚举测试】1Y )
Annoying painting tool 题目描述 Maybe you wonder what an annoying painting tool is? First of all, the pa ...
- RobotFramework教程使用笔记——Selenium2Library库
selenium之前已经学习介绍过了,它是一个支持多语言.多平台.多浏览器的web自动化测试框架,在robotframework中也可以导入selenium库来进行web自动化测试.它使用seleni ...
- MySQL学习笔记(三)——计算字段及常用函数
拼接字段-Concat()函数 将值连接在一起构成单个值.注意:大多数DBMS使用+或者||来实现拼接,mysql则使用Concat()函数来实现. 去空格函数-Trim函数 ...
- Java的Fork/Join任务,你写对了吗?
当我们需要执行大量的小任务时,有经验的Java开发人员都会采用线程池来高效执行这些小任务.然而,有一种任务,例如,对超过1000万个元素的数组进行排序,这种任务本身可以并发执行,但如何拆解成小任务需要 ...
- Fire Game
Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows ...