IOS8更新了,oc还将继续但新增了swift语言,能够代替oc编写ios应用,本文将使用swift作为编写语言,为大家提供step by step的教程。

工具

ios每次更新都须要更新xcode,这次也不例外,但使用xcode6,须要先升级到OS X 到Yosemite。具体的升级过程这里就不说了。
须要网盘下载的同学能够查看一下链接
http://bbs.pcbeta.com/viewthread-1516116-1-1.html

建立project

xocde开启后选择File->New->Project 建立新的project
新手教程自然选择Single View Application
Language 自然选择 Swift
建立好的project例如以下图所看到的:

Work With StoryBoard

StoryBoard是IOS5和xcode 4.2開始的新特性,使用storyboard会节省手机app设计的时间,将视图设计最大化,当然对于屌丝程序猿来说,什么board都无所谓。
箭头表示初始view。右下角的object library还是我们最熟悉的拖拽操作。
添加一个TableView到当前的ViewController。当然你也能够直接使用TableViewController。ViewController基本上与Android的Activity相似,都是View的控制器,用来编写View的逻辑。
好了,能够Run一下看看效果。

Hello world swift

最终能够開始swift之旅了,我们来看一下AppDelegate.swift文件。
//
//  AppDelegate.swift
//  swiftTableView
//
//  Created by Chi Zhang on 14/6/4.
//  Copyright (c) 2014年 Chi. All rights reserved.
//import UIKit@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow?func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {// Override point for customization after application launch.return true}func applicationWillResignActive(application: UIApplication) {// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}func applicationDidEnterBackground(application: UIApplication) {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}func applicationWillEnterForeground(application: UIApplication) {// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}func applicationDidBecomeActive(application: UIApplication) {// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}func applicationWillTerminate(application: UIApplication) {// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}}

是不是似曾相识,但组织上更像java和c#的逻辑,但别忘记骨子里还是object c。

打上一句hello world在application方法中。

println("helloworld")

能够Run一下看看效果,Application方法基本上与Android中的Application类似,都是App在開始载入时最先被运行的。
Swift的语法相对object c更加简洁,声明函数使用func开头,变量var开头,常量let开头,感觉上与javascript更加相似。

ViewController绑定TableView

最终到了本章最核心的内容了,怎样绑定数据到TableView。
首先我们看ViewController的代码:
//
//  ViewController.swift
//  swiftTableView
//
//  Created by Chi Zhang on 14/6/4.
//  Copyright (c) 2014年 Chi. All rights reserved.
//import UIKitclass ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}}

默认的ViewController仅仅提供了两个override的方法viewDidLoad和didReceiveMemoryWarning

我们须要让ViewController继承 UIViewController UITableViewDelegate UITableViewDataSource 三个类
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { ... }

添加完后,xcode会提示错误,当然不会像eclipse一样自己主动帮你加入必须的方法和构造函数,须要自行加入。

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {...}
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {...}

在ViewController中声明变量tableView用来管理我们之前在Storyboard中加入的tableView。

@IBOutlet
var tableView: UITableView

@IBOutlet 声明改变量暴露在Interface binder中。

在viewDidLoad时,在tableView变量中添加tableViewCell
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

声明一个String数组作为我们要绑定的数据

    var items: String[] = ["China", "USA", "Russia"]

在刚才声明的两个构造函数中填充逻辑。
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {return self.items.count;}func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCellcell.textLabel.text = self.items[indexPath.row]return cell}

好了,ViewController的部分基本上就写完了。然后我们切换回StoryBoard,将Referencing Outlet与ViewController进行连接,选择我们声明的变量tableView。

同一时候连接Outlets中的datasource和deletegate到ViewController。
以下是完整的ViewController代码:
//
//  ViewController.swift
//  swiftTableView
//
//  Created by Chi Zhang on 14/6/4.
//  Copyright (c) 2014年 Chi. All rights reserved.
//import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {@IBOutletvar tableView: UITableViewvar items: String[] = ["China", "USA", "Russia"]override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {return self.items.count;}func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCellcell.textLabel.text = self.items[indexPath.row]return cell}
}

好了,执行一下看看结果:

大功告成。当然我们还能够在ViewController中添加onCellClick的方法:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {println("You selected cell #\(indexPath.row)!")}

完整代码

转载于:https://www.cnblogs.com/gcczhongduan/p/4230639.html

swift入门之TableView相关推荐

  1. Swift入门篇-基本类型(1)

    原文:Swift入门篇-基本类型(1) 博主语文一直都不好(如有什么错别字,请您在下评论)望您谅解,没有上过什么学的 今天遇到了一个很烦的事情是,早上10点钟打开电脑,一直都进入系统(我的系统  ma ...

  2. Swift入门篇-循环语句

    Swift入门篇-循环语句 原文:Swift入门篇-循环语句 今天早上一起来所有新闻都是报道荷兰5-1战胜西班牙,我一看没有搞错吧,顿时想都如果中国队vs荷兰队也不至于会输的怎么惨吧,难道是荷兰队开挂 ...

  3. Swift入门[基于Java基础]

    Swift入门 学习目标 由于已经有了Java编程思想,所以着重了解Swift语言特有的特性,与Java不一样的地方.最终目的是可以使用Swift语言开发iOS应用. 学习过程 [阅读苹果官网Swif ...

  4. IOS开发语言Swift入门连载---类型转换

    IOS开发语言Swift入门连载-类型转换 类型转换可以判断实例的类型,也可以将实例看做是其父类或者子类的实例. 类型转换在 Swift 中使用is 和 as 操作符实现.这两个操作符提供了一种简单达 ...

  5. Swift 教程之TableView使用05 section的打开与关闭

    Swift 教程之TableView使用04section的打开与关闭 请点击,免费订阅<学Swift挣美元>专栏 之前系列课程 Swift 教程之TableView使用01基础代码 Sw ...

  6. IOS Swift 入门学习汇总 (更新中..)

    IOS Swift 学习入门 配置区 info 配置 本地化中文 文件导入Xcode CocoaPads 依赖管理工具 UI区 + 代码 通用 打开新页面方式 设置新开页面全屏展示 跳转页面 正向传值 ...

  7. Swift入门基础知识

    var //代表变量,变量的值可以改变 let//代表常量类型不可改变 //声明常量heh类型Swift会自动根据你的值来自动判断该变量的类型也可以指定类型(个人感觉还是指定类型的比较好,可能会减少系 ...

  8. Swift学习之--TableView的基本使用

    每个iOS开发人员都知道tableview,因为它是我们开发中最常用的Contrller了.下面就简单介绍Swift中tableview的一些简单的使用: (先看效果图) 下面直接贴代码 class ...

  9. Xcode Instruments调试swift入门教程

    无论您是在许多iOS应用程序上工作,还是仍在开始使用第一个应用程序:您无疑会想出新功能,并且想知道您可以做些什么来使您的应用程序更加出色 除了通过添加功能改进您的应用程序之外,所有优秀的应用程序开发人 ...

最新文章

  1. JSTL 及 tablibs 的简单介绍和配置方法
  2. Linux下root密码丢失和运行级别错误的解决办法
  3. python测试题 - 列表,字典,字符串
  4. UVA 818 Cutting Chains 切断圆环链 (暴力dfs)
  5. angular监听输入框值的变化_angular 实时监听input框value值的变化触发函数方法
  6. 深度学习入门——波士顿房价预测
  7. 有关文档流的一些注意事项
  8. 机器学习入门二 ----- 机器学习术语表
  9. MATLAB周边第三期-坤坤的唱跳rap
  10. 数据库原理与应用实验十 数据库完整性实验
  11. 如何注册域名的详细图文过程分享
  12. BZOJ 3270: 博物馆 1778: 驱逐猪猡 【概率DP+高斯消元】
  13. 北京职称计算机证书有效期,有关职称评审常见问题的解答(北京地区)
  14. 企业内网安全体系化发展方向
  15. 笔记本电脑常识:噪音
  16. 【记第一次kaggle比赛】PetFinder.my - Pawpularity Contest 宠物预测
  17. C/C++程序员面试指南
  18. Apache的配置与应用【Apache访问控制】以及apache日志管理【日志分割、awstats日志分析】
  19. php文件怎么打开? 教你用什么软件打开php文件
  20. Websphere8.5.5最新补丁包 :WebSphere Application Server V8.5.5 Fix Pack 15(8.5.5.15)

热门文章

  1. (Joomla)字符串截取
  2. 一个逐步“优化”的范例程序(转)
  3. android实时声音信号波形_Android输出正弦波音频信号(左右声道对称)-阿里云开发者社区...
  4. OpenCV测试程序
  5. OpenCV cvtColor()函数
  6. PYTHON-进阶-编码处理小结
  7. StarUML使用说明-指导手册
  8. MATLAB画图命令zz
  9. yii mysql 主从_mysql主从同步实践 YII
  10. Matlab将一矩阵中等于某个值的元素全部替换成另一个值