swift入门之TableView
工具
建立project
![](/assets/blank.gif)
![](/assets/blank.gif)
![](/assets/blank.gif)
![](/assets/blank.gif)
Work With StoryBoard
![](/assets/blank.gif)
![](/assets/blank.gif)
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 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
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 UITableViewCellcell.textLabel.text = self.items[indexPath.row]return cell}
好了,ViewController的部分基本上就写完了。然后我们切换回StoryBoard,将Referencing Outlet与ViewController进行连接,选择我们声明的变量tableView。
![](/assets/blank.gif)
![](/assets/blank.gif)
//
// 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}
}
好了,执行一下看看结果:
![](/assets/blank.gif)
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {println("You selected cell #\(indexPath.row)!")}
转载于:https://www.cnblogs.com/gcczhongduan/p/4230639.html
swift入门之TableView相关推荐
- Swift入门篇-基本类型(1)
原文:Swift入门篇-基本类型(1) 博主语文一直都不好(如有什么错别字,请您在下评论)望您谅解,没有上过什么学的 今天遇到了一个很烦的事情是,早上10点钟打开电脑,一直都进入系统(我的系统 ma ...
- Swift入门篇-循环语句
Swift入门篇-循环语句 原文:Swift入门篇-循环语句 今天早上一起来所有新闻都是报道荷兰5-1战胜西班牙,我一看没有搞错吧,顿时想都如果中国队vs荷兰队也不至于会输的怎么惨吧,难道是荷兰队开挂 ...
- Swift入门[基于Java基础]
Swift入门 学习目标 由于已经有了Java编程思想,所以着重了解Swift语言特有的特性,与Java不一样的地方.最终目的是可以使用Swift语言开发iOS应用. 学习过程 [阅读苹果官网Swif ...
- IOS开发语言Swift入门连载---类型转换
IOS开发语言Swift入门连载-类型转换 类型转换可以判断实例的类型,也可以将实例看做是其父类或者子类的实例. 类型转换在 Swift 中使用is 和 as 操作符实现.这两个操作符提供了一种简单达 ...
- Swift 教程之TableView使用05 section的打开与关闭
Swift 教程之TableView使用04section的打开与关闭 请点击,免费订阅<学Swift挣美元>专栏 之前系列课程 Swift 教程之TableView使用01基础代码 Sw ...
- IOS Swift 入门学习汇总 (更新中..)
IOS Swift 学习入门 配置区 info 配置 本地化中文 文件导入Xcode CocoaPads 依赖管理工具 UI区 + 代码 通用 打开新页面方式 设置新开页面全屏展示 跳转页面 正向传值 ...
- Swift入门基础知识
var //代表变量,变量的值可以改变 let//代表常量类型不可改变 //声明常量heh类型Swift会自动根据你的值来自动判断该变量的类型也可以指定类型(个人感觉还是指定类型的比较好,可能会减少系 ...
- Swift学习之--TableView的基本使用
每个iOS开发人员都知道tableview,因为它是我们开发中最常用的Contrller了.下面就简单介绍Swift中tableview的一些简单的使用: (先看效果图) 下面直接贴代码 class ...
- Xcode Instruments调试swift入门教程
无论您是在许多iOS应用程序上工作,还是仍在开始使用第一个应用程序:您无疑会想出新功能,并且想知道您可以做些什么来使您的应用程序更加出色 除了通过添加功能改进您的应用程序之外,所有优秀的应用程序开发人 ...
最新文章
- JSTL 及 tablibs 的简单介绍和配置方法
- Linux下root密码丢失和运行级别错误的解决办法
- python测试题 - 列表,字典,字符串
- UVA 818 Cutting Chains 切断圆环链 (暴力dfs)
- angular监听输入框值的变化_angular 实时监听input框value值的变化触发函数方法
- 深度学习入门——波士顿房价预测
- 有关文档流的一些注意事项
- 机器学习入门二 ----- 机器学习术语表
- MATLAB周边第三期-坤坤的唱跳rap
- 数据库原理与应用实验十 数据库完整性实验
- 如何注册域名的详细图文过程分享
- BZOJ 3270: 博物馆 1778: 驱逐猪猡 【概率DP+高斯消元】
- 北京职称计算机证书有效期,有关职称评审常见问题的解答(北京地区)
- 企业内网安全体系化发展方向
- 笔记本电脑常识:噪音
- 【记第一次kaggle比赛】PetFinder.my - Pawpularity Contest 宠物预测
- C/C++程序员面试指南
- Apache的配置与应用【Apache访问控制】以及apache日志管理【日志分割、awstats日志分析】
- php文件怎么打开? 教你用什么软件打开php文件
- Websphere8.5.5最新补丁包 :WebSphere Application Server V8.5.5 Fix Pack 15(8.5.5.15)
热门文章
- (Joomla)字符串截取
- 一个逐步“优化”的范例程序(转)
- android实时声音信号波形_Android输出正弦波音频信号(左右声道对称)-阿里云开发者社区...
- OpenCV测试程序
- OpenCV cvtColor()函数
- PYTHON-进阶-编码处理小结
- StarUML使用说明-指导手册
- MATLAB画图命令zz
- yii mysql 主从_mysql主从同步实践 YII
- Matlab将一矩阵中等于某个值的元素全部替换成另一个值