swift大体看了一遍,写个最常用的UItableview demo,希望对大家有所帮助,不对的地方也请大家批评指正。
1.用xcode建立一个基于swift语言的应用程序(xcode6.0可以进行oc和swift语言选择的)。
2.添加两个UIVIewcontroller、添加一个继承NSObject的自定义类,这样整个工程建立起来了,如下图:
3.先来看看StudentObject.swift代码:
import UIKit class StudentObject: NSObject { var sName:String? var sAge:Int? var sAddress:String? init(Name : String, Age : Int, Address : String) { sName = Name; sAge = Age; sAddress = Address; } }
4.再来看看TestTableViewController.swift代码:
import UIKit import Foundation class TestTableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { @IBOutlet var ui_myTable : UITableView = nil var tableData = StudentObject[](); init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) // Custom initialization } override func viewDidLoad() { super.viewDidLoad() self.title = "第一个页面"; for index in 1...10 { var tmpObj = StudentObject(Name: "lcc _ " + String(index) ,Age: index,Address: "address _ " + String(index)); tableData += tmpObj; } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { return tableData.count; } func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { let identifierString = "identifierString" var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifierString) as? UITableViewCell; if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.Default,reuseIdentifier: identifierString); println("%d",indexPath.row); } var tmpObj:StudentObject = tableData[indexPath.row]; cell.textLabel.text = tmpObj.sName; return cell; } func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat { return 100; }
//这里绑定单击事件和oc中一样 @IBAction func pushTapped(sender : AnyObject) { var secVc : SecondViewController = SecondViewController(nibName:nil,bundle: nil); self.navigationController.pushViewController(secVc,animated: true); } }
var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifierString) as? UITableViewCell;
a.选择类型和隐形选择类型。
b.?和!的区别。
c.as?和 as 是什么意思?
d.类型转换(大家可以自己把as后面的问好去掉,在运行试试)。
大家可以带着这些疑问可以区看看swift中文教程。不懂的,可以给我留言。
5.最后看看AppDelegate.swift
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow = UIWindow(frame: UIScreen.mainScreen().bounds) var testTable:TestTableViewController? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { self.testTable = TestTableViewController(nibName:nil, bundle:nil); var nav:UINavigationController = UINavigationController(rootViewController: self.testTable); self.window.rootViewController = nav; self.window.backgroundColor = UIColor.whiteColor() self.window.makeKeyAndVisible() return true } func applicationWillResignActive(application: UIApplication) { } func applicationDidEnterBackground(application: UIApplication) { } func applicationWillEnterForeground(application: UIApplication) { } func applicationDidBecomeActive(application: UIApplication) { } func applicationWillTerminate(application: UIApplication) { } }
源文件路径:http://download.csdn.net/detail/folish_audi/7516397