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中每一行结束后不须要加分号.多个语句在同一行内须要用分好隔开 //表示凝视.或者用/* - ...
随机推荐
- Service-监听手机来电
public class MonitorPhone extends Activity { TelephonyManager tManager; @Override protected void onC ...
- worktools-git 工具的使用总结(知识点累积)
1.用简单列表的方式查看提交记录git log --pretty=online zhangshuli@zhangshuli-MS-:~/myGit$ git log --pretty=oneline ...
- golang sync.Mutex
//go func 和主线程之间的关系是并行和竞争关系 package main import ( "fmt" "sync" "time" ...
- XAMPP各个版本配置
XAMPP各个版本配置 http://code.stephenmorley.org/articles/xampp-version-history-apache-mysql-php/ XAMPP Ap ...
- 24. Spring Boot 事务的使用
转自:https://blog.csdn.net/catoop/article/details/50595702
- 1.1 Introduction中 Distribution官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Distribution 分布式(Distribution) The partiti ...
- WebService学习总结(5)——WebService常见开发框架比较
在SOA领域,我们认为Web Service是SOA体系的构建单元(building block).对于服务开发人员来说,AXIS和CXF一定都不会陌生.这两个产品都是Apache孵化器下面的Web ...
- 【几何/数学】概念的理解 —— (非)刚体变换((non-)rigid transformation)
1. 刚体变换与非刚体变换 What is a non-rigid transformation? 刚体变换(rigid transformation)一般分为如下几种: 平移对象,而不改变形状和大小 ...
- WebService--CXF以及CXF与Spring的整合(jaxws:server形式配置)
前言:好记性不如烂笔头,写博客的好处是,以前接触的东西即便忘记了,也可以从这里查找. Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来 ...
- (转)高强度密码管理软件KeePass使用详解
转自:http://www.ruancan.com/ 算下来,你接触电脑有多久了?从第一次上网,到今天,你一共申请了多少个网站或者软件的帐号?相信这是一个几乎无人能够回答的问题. 无数人面临着这两个问 ...