在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. 把View转化成Image

    + (UIImage *) imageWithView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size ...

  2. 一种比较简单的在USB U盘中访问nandflash的方法

    u8 nandflash_write_buffer[NAND_SERECT_FULL_SIZE]; static int currentBlock = -1; static int currentPa ...

  3. ymodem协议c实现(转)

    源:ymodem协议c实现 /****************************************Copyright (c)******************************** ...

  4. IFrame跨域访问自定义高度

    由于JS禁止跨域访问,如何实现不同域的子页面将高度返回给父页面本身,是解决自定义高度的难点. JS跨域访问问题描述:应用A访问应用B的资源,由于A,B应用分别部署在不同应用服务器(tomcat)上,属 ...

  5. 在spring 3.0中的@value

    在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件 中的文件,进行键值对的注入,例子如下: 1 首先在applicationContext.xml中加入:   ...

  6. STM32-USB那点事

    STM32 USB那点事1 USB那点事2 - Custom HID例子程序解疑 USB那点事3 -使用端口2作为custom HID的传输 USB那点事5之USB通信出错 USB那点事6传输要素 S ...

  7. 报错:Can't bind to local 8647 for debugger

    修改8600为8601,并且勾上Use ADBHOST

  8. 使用命令创建github代码仓库,push本地仓库到github远程代码仓库

    1.利用命令创建github远程代码仓库 在将本地代码push到github远程代码仓库之前,总是需要新建github代码仓库,在将本地仓库关联到github远程仓库.其中最为繁琐的操作是建立gith ...

  9. bzoj3809

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3809 题目大意: Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题 ...

  10. linux在线预览pdf文件开发思路

    准备:swftools,flexpaper 基本思路: 1,将pdf文件转化成swf文件 2,使用flexpaper预览swf文件 主要代码: 1,在linux中安装swftools.官网下载swft ...