在IOS开发中,UItableView 的使用真的是最常见最普通的了,现在在自学swift 今天也是这用Swift 写了写 UItableview的使用,还有一些经常出错的地方。下面我先把整个控制器的代理列出来,大家可以顺便看看 swift 是怎样遵守协议的。

import UIKit
// 遵守两个协议
class HomeViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource { override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor=UIColor.whiteColor() // 创建tableview
creatTableview() // Do any additional setup after loading the view.
} // 创建tableView的方法
func creatTableview()
{ let tableview:UITableView = UITableView(frame: self.view.frame, style: UITableViewStyle.Plain)
tableview.delegate = self
tableview.dataSource = self // 这里要不注册,用下面的 dequeueReusableCellWithIdentifier 这个方法的时候会崩溃,这里和大家说一下,dequeueReusableCellWithIdentifier("SwiftCell", forIndexPath: indexPath) 要使用这个方法,在这里你就必须得先注册一个复用的cell,然后使用的时候 dequeueReusableCellWithIdentifier 会去调用能复用的cell tableview.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "SwiftCell")
self.view.addSubview(tableview) } // 组数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
} // 每组的个数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section
{
case 0: return 5 case 1: return 3 case 2: return 4 default: return 0
} } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("SwiftCell", forIndexPath: indexPath)
cell.textLabel?.text="你真的很帅"
return cell } func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 20
} // tableView是否能编辑,true 返回,就是能编辑
func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool { return true
} // 你要删除cell的时候,这个方法会自己调用的,这里一般就是在数据源里面删除cell 对应的数据。
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { print("删了这条数据了") } // 这里是编辑的类型
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { return UITableViewCellEditingStyle.Delete } // 这里是给 删除按钮上的 按钮文字进行重命名,这里是改成“别删呀”的名字。
func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? { return "别删呀" } /*
// MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/ }

特别说明几点内容:

1.自定义cell时,

若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib
不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:

2.需不需要注册?
使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;
使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。

Swift 2.0 UItableView 的简单使用的更多相关文章

  1. 【Swift 2.0】实现简单弹幕功能

    前言 简单实现弹幕功能,表跟我谈效率,但也有用队列控制同时弹的数量. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over ...

  2. swift 实践- 01 -- UItableView的简单使用

    import UIKit class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{ over ...

  3. Swift 3.0 Date的简单使用

    // // ViewController.swift // Date的使用 // // Created by 思 彭 on 16/9/20. // Copyright © 2016年 思 彭. All ...

  4. swift 3.0字符串的简单使用

    let str:String = "12314124" 获取某个指定位置的元素 print(str.characters[str.index(str.startIndex, off ...

  5. Swift 2.x -> Swift 3.0

    Swift 3.0 相对于 2.x 有很大变化.特别是因为命名习惯的改变,导致许多 Api 都发生了变化.总的趋势是让表示更简洁. 对旧的代码升级,大部分可以根据提示来进行更正.但也有的需要手动修改. ...

  6. Swift 2.0 异常处理

    转自:http://www.jianshu.com/p/96a7db3fde00 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主 ...

  7. fir.im Weekly - Swift 3.0 的迁移适配指南

    无论你是移动开发者,还是桌面端开发者,或者正在IoT领域探索的技术人员,那么应该更加关注 iDev 全平台开发者大会,也许是后半年 iOS 开发者最盛大的技术盛宴.既有知名公司带来专业视野,又有从 S ...

  8. Swift 3.0 令人兴奋,但Objective-C也有小改进--Objective-C的类属性

    由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.或许你会觉得苹果提及Objective-C 很可能是为了提高和Swift互操作性(译者注:互操作性主 ...

  9. Swift 2.0初探

    转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心. 今年6月,一年一度的WWDC大会如期而至,在大会上Apple发布了Swift 2.0,引 ...

随机推荐

  1. 51nod贪心算法教程

    51nod确实是一个好oj,题目质量不错,wa了还放数据,学习算法来说挺好的,这次我做了几个水的贪心,虽然水,但是确实都很典型. 教程链接:http://www.51nod.com/tutorial/ ...

  2. mysql 查找重复的数据

    Select Name,Count(*) From A Group By Name Having Count(*) > 1   Name是字段

  3. LRU Cache 题解

    题意 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  4. stm单片机之STM32F4-Discovery资料汇总 (转载自http://blog.163.com/thinki_cao/blog/static/83944875201362493134992/)

    STM32F4的资料大部分都在这里: http://www.stmcu.org/download/index.php?act=ziliao&id=150 根据个人的理解对这些资料作了一些规律, ...

  5. windowsxp系统下SVN添加新用户

    以我部署的文件为例: 我在f盘下新建一个zzz文件夹将其部署为svn共享工程后,新来员工需要添加svn账号以获取工程. 总共三步begin: 1.进入工程文件夹ZZZ在里面有一个conf文件夹如图: ...

  6. MySQL——数据类型

    MySQL中定义数据字段的类型对你数据库的优化是非常重要的.MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 一.字符串类型: 字符串类型指CHAR.VARCHAR.B ...

  7. 环信 之 iOS 客户端集成四:集成UI

    在Podfile文件里加入 pod 'EaseUI', :git => 'https://github.com/easemob/easeui-ios-cocoapods.git' 然后在终端中的 ...

  8. iOS 错误 之 http请求

     Application Transport Security has blocked a cleartext HTTP (http://) resource load since it is ins ...

  9. jmeter接口测试实践

    一.什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻 ...

  10. 2.7. 属性的各种设置选项(Core Data 应用程序实践指南)

    可供配置的选项根据属性类型的不同有所变化,并不是每一种属性都能配置下列选项 Transient:勾选该选项,表示该属性不写入“持久化存储区”,这听起来很奇怪,但有时候,只需要把特性留在托管对象上下文就 ...