swift入门之TableView
IOS8更新了,oc还将继续但新增了swift语言,能够代替oc编写ios应用,本文将使用swift作为编写语言,为大家提供step by step的教程。
工具
建立project
Work With StoryBoard
Hello world 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。
println("helloworld")
ViewController绑定TableView
//
// ViewController.swift
// swiftTableView
//
// Created by Chi Zhang on 14/6/4.
// Copyright (c) 2014年 Chi. All rights reserved.
// import UIKit class 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
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中。
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
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 UITableViewCell
cell.textLabel.text = self.items[indexPath.row]
return cell
}
好了,ViewController的部分基本上就写完了。然后我们切换回StoryBoard,将Referencing Outlet与ViewController进行连接,选择我们声明的变量tableView。
//
// ViewController.swift
// swiftTableView
//
// Created by Chi Zhang on 14/6/4.
// Copyright (c) 2014年 Chi. All rights reserved.
// import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet
var tableView: UITableView
var 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 UITableViewCell cell.textLabel.text = self.items[indexPath.row] return cell
}
}
好了,执行一下看看结果:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
}
swift入门之TableView的更多相关文章
- Swift入门学习之一常量,变量和声明
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/6059421.html Swift入门学习之一常量,变量和 ...
- Swift入门篇-闭包和函数
今天主要是给大家分享的是 swift中闭包的用法,我个人觉得闭包就是函数的简写方法,如果您函数不是很熟悉请查阅 swift入门篇-函数 1:函数类型 函数类型 var 变量 :(类型)->返回值 ...
- Swift入门篇-循环语句
今天早上一起来所有新闻都是报道荷兰5-1战胜西班牙,我一看没有搞错吧,顿时想都如果中国队vs荷兰队也不至于会输的怎么惨吧,难道是荷兰队开挂了,于是我看了一下昨天比赛的视频直播,还真是新闻报道的那样,顿 ...
- Swift入门篇-字符串和字符
今天主要是介绍一下字符串的用法 ,字符串的语法和object-c语法不太一样,但是思想是一样,就是写法不太一样.如果您对.net和java语法比较熟悉的话,那您几乎没有深压力.如果您对swift 基本 ...
- Swift入门篇-基本类型(2)
现在我也在学习Swift语言,常常去逛很多苹果社区和论坛,看到了圈子很多奇怪的现象,发现很多人都赶忙去翻译 Swift书籍 和 发布Swift的视频 .他们这种对新知识的探索精神我本人是很佩服的.但是 ...
- Swift入门篇-Hello World
提示:如果您使用手机和平板电脑看到这篇文章,您请在WIFI的环境下阅读,里面有很多图片, 会浪费很多流量. 博主语文一直都不好(如有什么错别字,请您在下评论)望您谅解,没有上过什么学的 最近这2天主要 ...
- Swift入门教程:基本语法大全
原文:Swift入门教程:基本语法大全 简介: ...
- Swift入门(五)——数组(Array)
集合 集合的定义 Swift中提供了两种数据结构用于存放数据的集合,各自是数组(Array)和字典(Dictionary). 他们的主要差别在于数组中的元素由下标确定.而字典中的数据的值由数据的键(K ...
- Swift入门(一)——基本的语法
近期開始学习swift.把学习的过程和总结整理成一个系列.方便日后回想总结. 基本的语法 基础语法 swift中每一行结束后不须要加分号.多个语句在同一行内须要用分好隔开 //表示凝视.或者用/* - ...
随机推荐
- Python修改文件内容
工作中要写个脚本来修改文件的内容,然后就写了一个刷子: #coding:utf8 import os def modify_file(old_file, new_version, old_versio ...
- [C++11] 默认构造函数
类通过一个特殊的构造函数来控制默认初始化过程.这个函数就是默认构造函数.默认构造函数无需不论什么实參. 我们能够显示的定义默认构造函数也能够让编译器为我们生成默认构造函数. 默认构造函数以例如以下规则 ...
- Android 上的 制表符(tab) —— 一个奇妙的字符 (二)
接到上回的说,主要是上回那个问题,我认为是android的bug,黎叔认为是cocos2dx的bug,叫我去提交bug.所以我又继续研究了下. 上回说到会调用java层的函数去创建一个image,然后 ...
- 01-Jvm 内存区域复习笔记
Java内存区域 1.程序计数器(Program Counter Register) 在虚拟机中一块较小的内存空间.它的作用能够看做是当前线程所运行的字节码的行号指示 ...
- 史上最简单,js并获取手机型号
原先获取不了苹果系列的型号,但转换思路,先推断是否是苹果,再用分辨率获取型号 //获取手机型号函数begin function getPhoneType(){ //正则,忽略大写和小写 var pa ...
- 27.mutex跨进程通信
创建互斥量mutex HANDLE mutex = CreateMutexA(NULL, TRUE, name); 根据id打开mutex HANDLE mutex = OpenMutexA(MUTE ...
- Spark通过YARN提交任务不成功(包含YARN cluster和YARN client)
无论用YARN cluster和YARN client来跑,均会出现如下问题. [spark@master spark-1.6.1-bin-hadoop2.6]$ jps 2049 NameNode ...
- BZOJ4817: [Sdoi2017]树点涂色(LCT)
Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...
- liunx中安装禅道
本文转自:https://www.cnblogs.com/bendouyao/p/10026746.html 一.准备工作 禅道安装包ZenTaoPMS.8.1.3.zbox_64.gz,上传至服务器 ...
- cx_Oracle
cx_Oracle 安装 pip install cx_Oracle 只是我没用那个安装成功过.我找了rpm 包. http://nchc.dl.sourceforge.net/project/cx- ...