下面的代码是使用的全部Xcode Version 6.0.1 (6A317)书面。

因为当使用团队开发stroyboard在并购的诸多不便的时间,所有或使用.xib该文件准备ToDo App.

想要实现的功能:TableView 够添加待做选项。并依照时间先后排序,能够实现删除。到点通知功能。

想要实现的效果例如以下:

      

步骤:

1、新建一个基于Singal View Application 的project,然后删掉storyboard,在新建两个新文件 Main.xib 和 Main.swift 作为基本的ViewController,打开 Main.xib 将 File's Owner的l类属性改为 Main(这样才干够将关联变量拖动到 Mian.swift )。

Main.xib 页面UI。一个用于展示todo list 的 tableView,然后关联一个 tableView 变量到 Main.swift文件

2、接下来设置 Mian 为rootViewController,在AppDelegate.swift中做写例如以下代码:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        var viewController = Main(nibName: "Main", bundle: nil)
navigationController = UINavigationController(rootViewController: viewController) self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible() return true
}

注意: var viewController =
Main(nibName:"Main", bundle: nil) ,用来将 Mian.xib 与 Mian.swift 进行绑定。run 一下你就能够看到界面了。

3、然后在Main.swift 中编写一下TableView 的数据源和代理的方法。这里我们用的是 自己定义的 Cell。全部新建一个 Cell.xib 和 Cell.swift 并将它们关联起来,做法和上面的同样,Cell.xib UI 例如以下。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? Cell
var str: String
if (cell == nil) {
let nibs:NSArray = NSBundle.mainBundle().loadNibNamed("Cell", owner: self, options: nil)
cell = nibs.lastObject as? Cell
} cell?.todoTitle.text = "toDoTitle"
cell?.time.text = "\(NSDate())"
cell?.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell!
} func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { } func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete { }
}

run 一下就能够看到例如以下效果:

注意:考虑到UITableView的滚动性能。Cell 的重用很重要,通过上面的 println(cell),滚动Cell,观察打印出来的 Cell 地址。能够看到 Cell 并没有进行重用。

override func viewDidLoad() { } 中加入以下的代码使 Cell 重用。

var bundle: NSBundle = NSBundle.mainBundle()
var nib: UINib = UINib(nibName: "Cell", bundle: bundle)
tableView.registerNib(nib, forCellReuseIdentifier: cellIdentifier)

4、以上讲到的都是些静态的数据,接下来我们做一些动态数据。

4.1、在NavigationBar 添加一个 ‘+’ button,用来给用户添加待做选项

self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addItem")

响应函数:

func addItem() {
let addVC: Add = Add(nibName: "Add", bundle: nil)
addVC.delegate = self;
self.presentViewController(addVC, animated: true, completion: nil)
}

4.2、新增一个 Add.xib 和 Add.swift 让用户输入待做选项。记得绑定(同步骤1),Add.xib UI例如以下:

为了在Main.swift 中接收到 Add.xib 中用户输入的信息,我们在 Add.swift 定义一个协议。然后Main.swift 遵循这个协议,在Add.xib 界面消失前获取用户输入信息。

protocol AddProtocal {
func didCompleted(addObject: Add)
}

Add.swift 代码例如以下:

//
// Add.swift
// ToDoApp
//
// Created by aaron on 14-9-17.
// Copyright (c) 2014年 The Technology Studio. All rights reserved.
// import UIKit protocol AddProtocal {
func didCompleted(addObject: Add)
} class Add: UIViewController { @IBOutlet var todo: UITextField!
@IBOutlet var desc: KCTextView!
@IBOutlet var time: UIDatePicker!
@IBOutlet var completeBtn: UIButton!
var delegate: AddProtocal? required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
} override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle? ) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
} override func viewWillAppear(animated: Bool) {
setup()
} func setup() {
completeBtn.layer.cornerRadius = 5.0
todo.placeholder = "请输入待做项"
// desc.placeholder = "请输入具体描写叙述。"
todo.text = self.todo.text
desc.text = self.desc.text
time.date = self.time.date
time.minimumDate = NSDate.date() if delegate? == nil {
todo.textColor = UIColor.lightGrayColor()
todo.userInteractionEnabled = false
desc.textColor = UIColor.lightGrayColor()
desc.userInteractionEnabled = false
time.userInteractionEnabled = false
completeBtn.setTitle("好", forState: UIControlState.Normal)
}else {
todo.textColor = UIColor.blackColor()
todo.userInteractionEnabled = true
desc.textColor = UIColor.blackColor()
desc.userInteractionEnabled = true
time.userInteractionEnabled = true
completeBtn.setTitle("完毕", forState: UIControlState.Normal)
} let swipeGesture = UISwipeGestureRecognizer(target: self, action:"hideKeyboard")
swipeGesture.direction = UISwipeGestureRecognizerDirection.Down
swipeGesture.numberOfTouchesRequired = 1
self.view.addGestureRecognizer(swipeGesture) } func hideKeyboard() {
println("swipeGesture....")
todo.resignFirstResponder()
desc.resignFirstResponder()
} func shakeAnimation(sender: AnyObject) {
let animation = CAKeyframeAnimation()
animation.keyPath = "position.x"
animation.values = [0, 10, -10, 10, 0]
animation.keyTimes = [0, 1/6.0, 3/6.0, 5/6.0, 1]
animation.duration = 0.4
animation.additive = true
sender.layer.addAnimation(animation, forKey: "shake")
} @IBAction func completeTouch(sender: AnyObject) {
if (countElements(todo.text) > 0){
delegate? .didCompleted(self)
self.dismissViewControllerAnimated(true, completion: nil)
}else{
shakeAnimation(todo)
}
}
@IBAction func editingDidEnd(sender: UITextField) {
if (countElements(sender.text) == 0) {
shakeAnimation(todo)
} } }

ToDo项为空时会有一个小小的提示动画:

Add.swift 中的关联变量 desc 是UITextView 类型的,UITextView 不像 UITextField 有 placeHolder ,所以这里我们引入一个 OC 写的 KCTextView ,由 KCTextView 取代 UITextView,swift 中引用 OC 写的 API easy,新建一个 .h 。把你须要用到的头文件统统写在里面,然后 Build Settings 中的 Object-C Bridging Header 写入 .h 文件的路径就可以。接着就能够正常使用
OC 写的接口了。

Main.swift 实现 AddProtocal,并实现协议规定的函数:

func didCompleted(addObject: Add) {

        toDoData.append(addObject)
tableView.reloadData()
}

toDoData的是一个 Add类型的可变数组。

Main.swift 代码例如以下:

//
// Main.swift
// ToDoApp
//
// Created by aaron on 14-9-16.
// Copyright (c) 2014年 The Technology Studio. All rights reserved.
// import UIKit class Main: UIViewController, UITableViewDataSource, UITableViewDelegate, AddProtocal { @IBOutlet var tableView: UITableView!
let cellIdentifier = "Cell" var toDoData = [Add]() override func viewDidLoad() {
super.viewDidLoad()
setup()
registerCell()
} func setup() {
self.title = "To Do List"
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addItem") } func registerCell() {
var bundle: NSBundle = NSBundle.mainBundle()
var nib: UINib = UINib(nibName: "Cell", bundle: bundle)
tableView.registerNib(nib, forCellReuseIdentifier: cellIdentifier)
} func addItem() {
let addVC: Add = Add(nibName: "Add", bundle: nil)
addVC.delegate = self;
self.presentViewController(addVC, animated: true, completion: nil)
} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return toDoData.count
} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? Cell
var str: String
if (cell == nil) {
let nibs:NSArray = NSBundle.mainBundle().loadNibNamed("Cell", owner: self, options: nil)
cell = nibs.lastObject as? Cell
} let addObject = toDoData[indexPath.row] as Add
cell? .todoTitle.text = addObject.todo.text
cell?.time.text = dateFormatter(addObject.time.date)
cell? .accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell!
} func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let addVC = toDoData[indexPath.row] as Add
addVC.delegate = nil
self.presentViewController(addVC, animated: true, completion: nil)
} func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
toDoData.removeAtIndex(indexPath.row)
tableView.reloadData()
}
} func didCompleted(addObject: Add) { toDoData.append(addObject)
toDoData.sort({ self.dateFormatter($0.time.date) < self.dateFormatter($1.time.date)})//按时间排序
tableView.reloadData() } func dateFormatter(date: NSDate) -> String {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.locale = NSLocale(localeIdentifier: NSGregorianCalendar)
let dateStr = formatter.stringFromDate(date)
return dateStr
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }

最后你大概能够看到这种效果:

5、最后一步,为待做项目加入通知功能,这一功能在之前的文章(ios8 notifacation in swift)中就讲过了,这里就不反复写了。完整的项目代码我发在github上来。须要的到这里拿。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Swift 书面 ToDo App的更多相关文章

  1. 用Swift语言做App开发之单元测试

    作为一个有质量保障的应用程序,当然少不了单元测试:Swift开发的App也亦如此,此文将以一个简单的实例来介绍Swift中的单元测试. 这里我们使用XCode模版自带的XCTest框架,此框架包含了一 ...

  2. 21个高质量的Swift开源iOS App

    原文:21 Amazing Open Source iOS Apps Written in Swift 对Swift初学者来说,学习开源项目,阅读源码是个不错的方法.在这篇文章中,基于对代码质量和排名 ...

  3. Meteor ToDo App实例

    在本章中,我们将创建一个简单的待办事项应用程序. 第1步 - 创建应用程序 打开命令提示符,运行以下命令 - C:\Users\Administrator\Desktop>meteor crea ...

  4. react native 实现TODO APP

    前端有一个todo app非常适合入门练手 react-native 实现todo app:https://github.com/nwgdegitHub/TODO_RN.git

  5. 写一个TODO App学习Flutter本地存储工具Moor

    写一个TODO App学习Flutter本地存储工具Moor Flutter的数据库存储, 官方文档: https://flutter.dev/docs/cookbook/persistence/sq ...

  6. .NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App

    作者:Steven Giesel 翻译:Alan Wang 校对:李卫涵 – 微软 MVP 排版:Rani Sun 有什么比参考包含分步说明和代码示例的动手教程更好的学习新技术的方式呢?当你完成或 f ...

  7. 使用 Realm 和 Swift 创建 ToDo 应用

    原文出处: HOSSAM GHAREEB   译文出处:Prayer’s blog(@EclipsePrayer) 智能手机的快速发展的同时,涌现出了很多对开发者友好的开发工具,这些工具不仅使得开发变 ...

  8. Building gRPC Client iOS Swift Note Taking App

    gRPC is an universal remote procedure call framework developed by Google that has been gaining inter ...

  9. 【转载】谷歌酝酿将苹果Swift作为安卓APP主要开发语言

    TNW中文站 4月8日报道 安卓操作系统的软件开发语言是Java,而在过去几年中,有关Java的版权,谷歌(微博)和甲骨文之间发生了长期的诉讼.最新外媒消息称,谷歌正在考虑将苹果开发的Swift作为未 ...

随机推荐

  1. 谈Web应用系统的可维护性

           每一个软件开发人员都十分清楚, 当软件构建得越来越复杂时, 可维护性就成了一个很突出的问题. 如何在构造软件系统的过程中始终保持可控制的可维护性呢?          一. 整体组织   ...

  2. HTML学习笔记之中的一个(input文件选择框的封装)

    方式一:直接透明隐藏 .file_button_container,.file_button_container input {background: transparent url(./img/BT ...

  3. 下拉刷新,上拉装载许多其他ListView

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanVuaHVhaG91c2U=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. Mvc 异常处理 ajax的 和 不是ajax的!

    using ImageUpload.Auth; using System; using System.Collections.Generic; using System.Linq; using Sys ...

  5. Swift的属性,方法,下标脚本以及继承

    从这篇章节起,Swift编程语言指南大部分的重要内容在于概念,代码并非太多.理解Swift的面向对象理念,语法以及类结构,构造析构过程对于非常好的应用Swift语言将会有比較大的帮助. 属性 存储属性 ...

  6. duilib底层机制剖析:窗口类与窗口句柄的关联

    转载请说明原出处.谢谢~~ 看到群里朋友有人讨论WTL中的thunk技术,让我联想到了duilib的类似技术. 这些技术都是为了解决c++封装的窗口类与窗口句柄的关联问题. 这里是三篇关于thunk技 ...

  7. Java设计模式之认识阶段

    设计模式是什么? 设计模式(Design pattern)是一套被重复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 其本质就是继承与接口的组合应用. 为什么要用设计模? 使用设计模式是为了 ...

  8. Python学习入门基础教程(learning Python)--3.3.3 Python逻辑关系表达式

    在if分支判断语句里的条件判断语句不一定就是一个表达式,可以是多个(布尔)表达式的组合关系运算,这里如何使用更多的关系表达式构建出一个比较复杂的条件判断呢?这里需要再了解一下逻辑运算的基础知识.逻辑关 ...

  9. WPF换肤之二:可拉动的窗体

    原文:WPF换肤之二:可拉动的窗体 让我们接着上一章: WPF换肤之一:创建圆角窗体 来继续. 在这一章,我主要是实现对圆角窗体的拖动,改变大小功能. 拖动自绘窗体的步骤 首先,通过上节的设计,我们知 ...

  10. 让浏览器支持 jquery ajax load 前进、后退 功能

    BEGIN; 一般在做 ajax load 的时候,非常多人都不会考虑到须要浏览器支持前进后退功能,由于大部分人都不知道能够实现. 近期遇到这个问题,经过一小段研究,发现github已经有现成的开源工 ...