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的更多相关文章

  1. Swift 语言概览 -自己在Xcode6 动手写1

    原文:Swift 语言概览 -自己在Xcode6 动手写1 Swift是什么? Swift是苹果于WWDC 2014发布的编程语言,这里引用The Swift Programming Language ...

  2. Swift 编程语言自己实践 -自己在Xcode6 动手写20140603

    Swift 是什么,大家都回去百度或者Google,有的甚至认为是Taylor Swift(她是我的偶像),但是如果今天在百度百科里搜索绝对没有说是Apple最新推出的编程语言,因为是在2014年6月 ...

  3. Swift语言概览

                     Swift语言概览 关于 这篇文章简要介绍了苹果于WWDC 2014公布的编程语言--Swift. 前言 在这里我觉得有必要提一下Brec Victor的Invent ...

  4. SWIFT语言的概览

    Swift用来写iOS和OS X程序.(估计也不会支持其它屌丝系统) Swift吸取了C和Objective-C的优点,且更加强大易用. Swift可以使用现有的Cocoa和Cocoa Touch框架 ...

  5. ios Swift 动手写

    Swift语言概览 基本概念 注:这一节的代码源自The Swift Programming Language中的A Swift Tour. Hello, world 类似于脚本语言,下面的代码即是一 ...

  6. Swift语言 1小时速学教程

    本文由 张渊杰 (网名寂静)编写 Swift语言 1小时速学教程 写在前面的话 有些人可能想, 呵呵, 1小时学一门语言, 你不是搞笑吧, 我想说, 是的, 完全可以, 就要看你怎么学了 要想在1小时 ...

  7. 【转】从Go、Swift语言出发

    Google于2009年第一次提出了Go的构思,Facebook在去年春天引入了Hack,随后不久Apple也发布了其Swift语言. 在战争中,胜利者写历史书:在科技中,赢的公司都在写编程语言.互联 ...

  8. swift语言之多线程操作和操作队列(下)———坚持51天吃掉大象(写技术文章)

    欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...

  9. 【转】自己动手写SC语言编译器

    自序 编译原理与技术的一整套理论在整个计算机科学领域占有相当重要的地位,学习它对程序设计人员有很大的帮助.我们考究历史会发现那些人人称颂的程序设 计大师都是编译领域的高手,像写出BASIC语言的BIL ...

随机推荐

  1. 注意string的insert函数的几种形式

    string (1) string& insert (size_t pos, const string& str); substring (2) string& insert ...

  2. 摆脱技术思维,转向产品思维——寻找“万能”IDC的苦恼

    背景:近期在新产品的开发任务完毕后一直在为寻找好的IDC和优质的托管服务忙碌.需求源自于我们重点要解决之前老版产品面临的国内外用户訪问速度慢甚至连接不上的问题. 除去架构技术上使用高性能.可扩展的方案 ...

  3. [NOI.AC#41]最短路 线性基

    链接 题解 如果不加边,两个点之间的长度是唯一的(只能走最短路径),因为如果重复走,就异或掉了. 因此,先DFS预处理一下每个点到根的距离 \(d[x]\) ,那么 \(x,y\) 之间的距离为 $d ...

  4. 37.Node.js工具模块---处理和转换文件路径的工具 Path模块

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js path 模块提供了一些用于处理文件路径的小工具,我们可以通过以下方 ...

  5. python路径找类并获取静态字段

    Python通过路径找类并获取其中大写的静态字段 settings.py class Foo: DEBUG = True TEST = True xx.py import importlib path ...

  6. 设计模式之十二:状态模式(State)

    状态模式: 当一个对象的内部状态发生变化时同意改变它的行为. Allow an object to alter its behavior when its internal state changes ...

  7. Gmail 收信的一些规则

    Gmail 收信的一些规则 用 apache+php+MDaemon 调试 mail2www 时,发往gmail的邮件失败, 提示: Our system detected an illegal at ...

  8. rocketmq事务消息入门介绍

    说明 周五的时候发了篇:Rocketmq4.3支持事务啦!!!,趁着周末的时候把相关内容看了下,下面的主要内容就是关于RocketMQ事务相关内容介绍了. 说明: 今天这篇仅仅是入门介绍,并没有涉及到 ...

  9. R语言-回归

    定义: 回归是统计学的核心,它其实是一个广义的概念,通常指那些用一个或多个预测变量来预测响应变量.既:从一堆数据中获取最优模型参数 1.线性回归 1.1简单线性回归 案例:女性预测身高和体重的关系 结 ...

  10. android Email总结文档

    目录:src\com.android.email.activity 一. Welcome.java 根据AndroidManifest.xml可知该文件为程序入口文件: 加载该文件时,查询数据库账户列 ...