学了这么久的swift语法和相关技术,今天忍不住手痒痒就写了一个swift的小项目,这个项目非常简单(只是使用一个UITableView),但是里面的功能却非常有用。

我们要实现的功能是这样的:

程序启动之后就会出现主界面,在主界面有一些默认的度假地

点击编辑就会出现能过编辑的界面(这里编辑只实现了删除,修改没有处理,增加在+实现了),也可以手指向左拖动实现,

点击+可新增一个项目行

是不是很简单?

下面就看看代码时证明实现的

一;定义一个类用来处理对应的属性

 import Foundation

 class Vocation {
     var place = ""
     var visited = false
 }

二:在主界面嘞中实现下面代码

 import UIKit

 class ListTableViewController: UITableViewController {
     //度假地数组
     var vocationList = [Vocation]()

     //添加度假地
     func loadInitListData() {
         /**
          类的初始化,初始化五个度假地,设置度假地的地名,并且添加到列表中
         */
         let vocation1 = Vocation()
         vocation1.place = "北京"
         vocationList.append(vocation1)

         let vocation2 = Vocation()
         vocation2.place = "上海"
         vocationList.append(vocation2)

         let vocation3 = Vocation()
         vocation3.place = "深圳"
         vocationList.append(vocation3)

         let vocation4 = Vocation()
         vocation4.place = "广州"
         vocationList.append(vocation4)

         let vocation5 = Vocation()
         vocation5.place = "南京"
         vocationList.append(vocation5)

     }

     override func viewDidLoad() {
         super.viewDidLoad()
         //导航栏左边的按钮
         self.navigationItem.leftBarButtonItem = self.editButtonItem()

         //初始化
         loadInitListData()
     }

     override func setEditing(editing: Bool, animated: Bool) {
         super.setEditing(editing, animated: true)
         //设置表格可以编辑
         tableView.setEditing(editing, animated: true)

     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
     }

     override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
         //返回一节数据

     }

     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         //返回数组中数据的数量
           return   vocationList.count
     }

     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         //重用机制,我们需要在单元格中设置对应的Identifier
         let cell = tableView.dequeueReusableCellWithIdentifier("ListID", forIndexPath: indexPath) as! UITableViewCell
         //保存状态
     let v = vocationList[indexPath.row]
         //根据数组的索引出来的地点来填充数据到对应的行
         cell.textLabel?.text = v.place
         //判断单元格的状态
         if v.visited {
             //如果已经参观过则点击一下现实一个勾
             cell.accessoryType = UITableViewCellAccessoryType.Checkmark
         }else {
             //如果没有去过可以取消点击,去掉那个勾
             cell.accessoryType = UITableViewCellAccessoryType.None

             /**UITableViewCellAccessoryType对应的属性,这里是一个枚举(enmu:case)
             case None // don't show any accessory view
             case DisclosureIndicator // regular chevron. doesn't track
             case DetailDisclosureButton // info button w/ chevron. tracks
             case Checkmark // checkmark. doesn't track
             @availability(iOS, introduced=7.0)
             case DetailButton // info button. tracks
             */
         }

         return cell
     }

     //选择行
     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         //取出选择的对应行数据
         let v1 = vocationList[indexPath.row]
         //设置已经去过的地方,这里使用取反实现
         v1.visited = !v1.visited
         //取消选中状态
         tableView.deselectRowAtIndexPath(indexPath, animated: false)
         //刷新表格
         tableView.reloadData()

     }

     // 是否能够编辑
     override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
         return true
     }

     override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
         //如果是删除状态
         if editingStyle == .Delete {    //省略了UITableViewCellEditingStyle
             //根据索引行删除数据
             vocationList.removeAtIndex(indexPath.row)
             //根据索引行删除单元格
             tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

             /**UITableViewRowAnimation对应的属性,这里是一个枚举
             case Fade
             case Right // slide in from right (or out to right)
             case Left
             case Top
             case Bottom
             case None // available in iOS 3.0
             case Middle // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy
             case Automatic // available in iOS 5.0.  chooses an appropriate animation style for you
             */

             //如果是插入状态
         } else if editingStyle == .Insert {

         }
     }

     // Override to support rearranging the table view.
     override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

     }

     //是否能够移动
     override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
         return true
     }

     //prepareForSegue方法
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

     }

     //回到主界面的方法
     @IBAction func unwindToList(segue: UIStoryboardSegue){
         //定义一个源控制器为我们自定义的控制器
         let source = segue.sourceViewController as! AddViewController
         let va = source.vocation
         //判断度假地是否为空
         if va.place != "" {
             vocationList.append(va)
         }

     }

 }

实现点击新增(+)界面的代码

 class AddViewController: UIViewController {
     //初始化类
     var vocation = Vocation()

     @IBOutlet weak var text: UITextField!
     //@IBOutlet weak var textField:UItextField!

     @IBOutlet weak var done: UIBarButtonItem!
     //@IBOutlet weak var doneBtn:UIBarButtonItem!

     override func viewDidLoad() {
         super.viewDidLoad()

     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
     }

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

         if  sender as! NSObject == done {
             if (!text.text.isEmpty) {
                 vocation.place = text.text
             }
         }
     }
 

ios开发——实战Swift篇&简单项目的实现的更多相关文章

  1. iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController)

    iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController)   前面我们介绍了StoryBoard这个新技术,和纯技术 ...

  2. iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)

    iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)   这里我们就直接上实例: 一:新建一个项目singleV ...

  3. iOS开发——实战OC篇&环境搭建之StoryBoard(玩转UINavigationController与UITabBarController)

      环境搭建之StoryBoard(玩转UINavigationController与UITabBarController)   研究了这么就IOS开发,都没有所处一个像样或者自己忙一点的项目.最近自 ...

  4. iOS 开发——实用技术Swift篇&Swift 懒加载(lazy)

    Swift 懒加载(lazy) 在程序设计中,我们经常会使用 * 懒加载 * ,顾名思义,就是用到的时候再开辟空间,比如iOS开发中的最常用控件UITableView,实现数据源方法的时候,通常我们都 ...

  5. iOS开发——网络Swift篇&NSURL进行数据请求(POST与GET)

    NSURL进行数据请求(POST与GET)   使用Swift进行iOS开发时,不可避免的要进行远程的数据获取和提交. 其数据请求的方式既可能是POST也可能是GET.同不管是POST还是GET又可以 ...

  6. IOS开发实战-Xcode创建HelloWorld项目

    一.创建工程打开Xcode开发工具,在Welcome界面选择”Create a new Xcode project”选项 在选择模板窗口,选择”Single View Application” 确定模 ...

  7. iOS开发——实用技术OC篇&简单抽屉效果的实现

    简单抽屉效果的实现 就目前大部分App来说基本上都有关于抽屉效果的实现,比如QQ/微信等.所以,今天我们就来简单的实现一下.当然如果你想你的效果更好或者是封装成一个到哪里都能用的工具类,那就还需要下一 ...

  8. iOS开发——网络Swift篇&JSON与XML数据解析

    JSON与XML数据解析 JSON数据解析(内置NSJSONSerialization与第三方JSONKit)   一,使用自带的NSJSONSerialization 苹果从IOS5.0后推出了SD ...

  9. ios开发——实战OC篇&FMDB详解

    FMDB详解 前一篇文章中我们介绍的SQLite的使用,在iOS中原生的SQLite API在使用上相当不友好. 于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.Plausibl ...

随机推荐

  1. [Everyday Mathematics]20150125

    试求极限 $$\bex \lim_{x\to 0^+}\int_x^{2x} \frac{\sin^m t}{t^n}\rd t\quad\sex{m,n\in\bbN}. \eex$$

  2. C#中的值类型(value type)与引用类型(reference type)的区别

    ylbtech- .NET-Basic:C#中的值类型与引用类型的区别 C#中的值类型(value type)与引用类型(reference type)的区别 1.A,相关概念返回顶部     C#中 ...

  3. Linux基本命令(4)有关关机和查看系统信息的命令

    有关关机和查看系统信息的命令 命令 说明 shutdown 正常关机 reboot 重启计算机 ps 查看目前程序执行的情况 top 查看目前程序执行的情景和内存使用的情况 kill 终止一个进程 d ...

  4. nginx+tomcat反向代理下使用tomcat-redis-session-manager进行session共享中值得注意的一个问题

    公司目前项目使用nginx反向代理+多个tomcat进行负载均衡,之前使用ip_hash策略进行session控制.近期有考虑不再使用ip_hash策略,所以需要进行session共享. 根据项目实际 ...

  5. 【原】Storm 入门教程目录

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  6. dfs.datanode.max.xcievers参数导致hbase集群报错

    2013/08/09 转发自http://bkeep.blog.163.com/blog/static/123414290201272644422987/ [案例]dfs.datanode.max.x ...

  7. JAVA——利用wait和notify实现生产者和消费者

    经典的消费者和生产者的的实现: 注意事项: 1:在循环里面用wait(),因为当线程获得了锁,但是有可能还没有满足其他条件: 2:公用的缓冲池要用锁机制: package demo; import j ...

  8. 第二百三十七天 how can I 坚持

    最近好像迷上看小说了,<灵域>,而且也感觉会看小说了. 话说,今天好冷啊,真怕在路上冻着就冻萌了,寒风赤骨啊. 好想买个帽子.好想让送个帽子. 睡觉.

  9. 现代C++作业2 与 围棋homework-06

    本文第一部分是现代C++作业2,第二部分是对围棋程序的部分建议,还有一些修改和优化体现在Github里面的代码中. 首先是现代C++作业. 1. 了解Lambda的用法.计算“Hello World! ...

  10. HDU 2296 Ring (AC自动机+DP)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...