Swift 语言概览 -自己在Xcode6 动手写2-tableView
2014-06-04 14:49
260 查看
import UIKit class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource { var tableView : UITableView? var items :NSMutableArray? var leftBtn:UIButton? override func viewDidLoad() { super.viewDidLoad() self.title = "I love Swift" self.items = NSMutableArray() // self.items?.addObject("1","2") // Do any additional setup after loading the view, typically from a nib. setupViews() setupRightBarButtonItem() setupLeftBarButtonItem(); } func setupViews() { self.tableView = UITableView(frame:self.view!.frame) self.tableView!.delegate = self self.tableView!.dataSource = self self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") self.view?.addSubview(self.tableView) } func setupLeftBarButtonItem() { self.leftBtn = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton self.leftBtn!.frame = CGRectMake(0,0,50,40) self.leftBtn?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal) self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal) self.leftBtn!.tag = 100 self.leftBtn!.userInteractionEnabled = false self.leftBtn?.addTarget(self, action: "leftBarButtonItemClicked", forControlEvents: UIControlEvents.TouchUpInside) var barButtonItem = UIBarButtonItem(customView: self.leftBtn) self.navigationItem!.leftBarButtonItem = barButtonItem } func setupRightBarButtonItem() { var barButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self, action: "rightBarButtonItemClicked") self.navigationItem!.rightBarButtonItem = barButtonItem } func rightBarButtonItemClicked() { var row = self.items!.count var indexPath = NSIndexPath(forRow:row,inSection:0) self.items?.addObject("1") self.tableView?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left) self.leftBtn!.userInteractionEnabled = true } func leftBarButtonItemClicked() { if (self.leftBtn!.tag == 100) { self.tableView?.setEditing(true, animated: true) self.leftBtn!.tag = 200 self.leftBtn?.setTitle("Done", forState: UIControlState.Normal) } else { self.tableView?.setEditing(false, animated: true) self.leftBtn!.tag = 100 self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal) } } 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! { let cell = tableView .dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell cell.textLabel.text = String(format: "%i", indexPath.row+1) return cell } func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool { return true } func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) { self.items?.removeObjectAtIndex(indexPath.row) self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top) if (self.items!.count == 0) { self.leftBtn!.userInteractionEnabled = false } } func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle { return (UITableViewCellEditingStyle.Delete) } func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool { return true } func tableView(tableView: UITableView!, moveRowAtIndexPath sourceIndexPath: NSIndexPath!, toIndexPath destinationIndexPath: NSIndexPath!) { self.tableView?.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath) self.items?.exchangeObjectAtIndex(sourceIndexPath.row, withObjectAtIndex: destinationIndexPath.row) } func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { println("row = %d",indexPath.row) } }
分享一个官网文档的链接,有兴趣的童鞋可以去看看,有关OC 和 Swift 的兼容问题
https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75
相关文章推荐
- Swift 语言概览 -自己在Xcode6 动手写1
- Swift 语言概览 -自己在Xcode6 动手写1
- Swift 编程语言自己实践 -自己在Xcode6 动手写20140603
- swift调用oc语言文件,第三方库文件或者自己创建的oc文件——简书作者
- Apple Swift语言基础入门 —— Swift概览1
- 你知道为什么Xcode6中Swift没有智能提示和自己主动补全功能吗 ?
- Swift语言中手写代码执行动画效果
- Swift语言概览
- Swift语言基本概览
- Apple Swift语言基础入门 —— Swift概览5
- Apple Swift语言基础入门 —— Swift概览4
- Swift 语言概览
- Swift高级开发语言--函数嵌套
- The Swift Programming Language--语言指南--基本运算符
- 移动开发四国语言概览二:标识符
- Perl 语言概览
- 【手写数字图片预处理】用skimage将自己的手写数字转换为mnist数据集
- 自己动手写缓存系统(tmcache)
- swift语言的学习笔记四-2(对上一节有些遗留进行处理)
- 零基础上手Swift快速入门教程概览