Swift 语言概览 -自己在Xcode6 动手写2-tableView
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(,,,)
self.leftBtn?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)
self.leftBtn!.tag =
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:)
self.items?.addObject("")
self.tableView?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
self.leftBtn!.userInteractionEnabled = true
} func leftBarButtonItemClicked()
{
if (self.leftBtn!.tag == )
{
self.tableView?.setEditing(true, animated: true)
self.leftBtn!.tag =
self.leftBtn?.setTitle("Done", forState: UIControlState.Normal)
}
else
{
self.tableView?.setEditing(false, animated: true)
self.leftBtn!.tag =
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+)
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 == )
{
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 动手写2-tableView的更多相关文章
- Swift 语言概览 -自己在Xcode6 动手写1
原文:Swift 语言概览 -自己在Xcode6 动手写1 Swift是什么? Swift是苹果于WWDC 2014发布的编程语言,这里引用The Swift Programming Language ...
- Swift 编程语言自己实践 -自己在Xcode6 动手写20140603
Swift 是什么,大家都回去百度或者Google,有的甚至认为是Taylor Swift(她是我的偶像),但是如果今天在百度百科里搜索绝对没有说是Apple最新推出的编程语言,因为是在2014年6月 ...
- Swift语言概览
Swift语言概览 关于 这篇文章简要介绍了苹果于WWDC 2014公布的编程语言--Swift. 前言 在这里我觉得有必要提一下Brec Victor的Invent ...
- SWIFT语言的概览
Swift用来写iOS和OS X程序.(估计也不会支持其它屌丝系统) Swift吸取了C和Objective-C的优点,且更加强大易用. Swift可以使用现有的Cocoa和Cocoa Touch框架 ...
- ios Swift 动手写
Swift语言概览 基本概念 注:这一节的代码源自The Swift Programming Language中的A Swift Tour. Hello, world 类似于脚本语言,下面的代码即是一 ...
- Swift语言 1小时速学教程
本文由 张渊杰 (网名寂静)编写 Swift语言 1小时速学教程 写在前面的话 有些人可能想, 呵呵, 1小时学一门语言, 你不是搞笑吧, 我想说, 是的, 完全可以, 就要看你怎么学了 要想在1小时 ...
- 【转】从Go、Swift语言出发
Google于2009年第一次提出了Go的构思,Facebook在去年春天引入了Hack,随后不久Apple也发布了其Swift语言. 在战争中,胜利者写历史书:在科技中,赢的公司都在写编程语言.互联 ...
- swift语言之多线程操作和操作队列(下)———坚持51天吃掉大象(写技术文章)
欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...
- 【转】自己动手写SC语言编译器
自序 编译原理与技术的一整套理论在整个计算机科学领域占有相当重要的地位,学习它对程序设计人员有很大的帮助.我们考究历史会发现那些人人称颂的程序设 计大师都是编译领域的高手,像写出BASIC语言的BIL ...
随机推荐
- Gym - 100502A Amanda Lounges
Amanda Lounges Time Limit: 1000MS Memory Limit: 524288KB 64bit IO Format: %I64d & %I64u AMAN ...
- BZOJ2733: [HNOI2012]永无乡(线段树合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- CISP/CISA 每日一题 11
CISA 每日一题(答) 一个合理建造的数据仓库应当支持下列三种基本的查询格式: 1.向上溯源和向下溯源——向上溯源是对数据进行总计:向下溯源是将数据进行细化: 2.交叉溯源——通过通用属性访问数据仓 ...
- Native开发与JNI机制详解
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 文/李森 博主导读:博主不是代码大师,研究什么都很深,Java除外,所以无论什么问题,方案可以提供, ...
- [Javascript] Different ways to create an new array/object based on existing array/object
Array: 1. slice() const newAry = ary.slice() 2. concat const newAry = [].concat(ary) 3. spread oprea ...
- dot-files/directories 点开头的文件或文件夹(windows/linux)
What's so special about directories whose names begin with a dot? 不管是 windows 系统,还是类 linux 系统,以点开头的文 ...
- AndroidStudio MAT LeakCanary 内存分析之 LeakCanary
现在我们换一种更清晰方便的方式:LeakCanary https://github.com/square/leakcanary 首先将LeakCanary绑在我们的app上 build.gradle ...
- android 4.4 添加物理按键
kernel下添加 Linux-3.4/drivers/input/keyboard/Makefile linux-3.4/drivers/input/keyboard/sw-keyboard.c s ...
- Android开发之搜芽项目的图片载入问题(使用Volley进行网络图片载入)
搜芽的移动开发这几天进度相对来说很的快. 可是美中不足的就是网络图片的载入问题. 我有两套方案: 1)沿用迅雷动漫的图片载入.迅雷动漫也是用的一个开源的库.可是不知道是我使用出了问题还是真的是它的问题 ...
- 关于YUM错误,Error: rpmdb open failed
错误如题: rpmdb: Thread/process / failed: Thread died in Berkeley DB library error: db3 error(-) from db ...