标签:
创建工程时语言勾选Swift
AppDelegate.swift
1 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 2 // Override point for customization after application launch. 3 // 创建window 4 self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 5 self.window?.backgroundColor = UIColor.whiteColor() 6 self.window?.makeKeyAndVisible() 7 8 let rootVC : ContactListTableViewController = ContactListTableViewController() 9 let rootNVC : UINavigationController = UINavigationController(rootViewController: rootVC) 10 // 设置根视图控制器 11 self.window?.rootViewController = rootNVC 12 13 return true 14 }
ContactListTableViewController.swift 首页 -- 展示联系人列表
1 import UIKit 2 3 class ContactListTableViewController: UITableViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 self.title = "通讯录" 8 // 设置导航控制器样式 9 self.navigationController?.navigationBar.barStyle = UIBarStyle.Black 10 self.navigationController?.navigationBar.backgroundColor = UIColor.blackColor() 11 self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() 12 // 左按钮 -- 添加联系人按钮 13 self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: Selector("addButtonAction")) 14 // 右按钮 -- 编辑 15 self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "编辑", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editAction")) 16 17 } 18 19 // MARK: - 添加按钮点击事件 20 func addButtonAction() -> Void { 21 // 跳转新的页面 22 let addVC = AddContactViewController() 23 // 实现闭包的回调 24 addVC.myBlock = { 25 () -> Void in 26 self.tableView.reloadData() 27 } 28 self.navigationController?.pushViewController(addVC, animated: true) 29 30 } 31 32 // MARK: - 编辑按钮点击事件 33 func editAction() { 34 self.tableView.setEditing(!self.tableView.editing, animated: true) 35 } 36 37 // MARK: - Table view data source 38 // MARK: - 分区数 39 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 40 return 1 41 } 42 43 // MARK: - 分区行数 44 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 45 // #warning Incomplete implementation, return the number of rows 46 return DataHandle.shareInstence.count 47 } 48 49 // MARK: - 返回cell 50 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 51 52 var cell = tableView.dequeueReusableCellWithIdentifier("cell") 53 if cell == nil { 54 cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell") 55 } 56 let contact = DataHandle.shareInstence.findContactWithIndex(indexPath.row) 57 cell?.textLabel?.text = contact.name 58 cell?.detailTextLabel?.text = contact.phone 59 return cell! 60 } 61 62 // MARK: - 点击cell 63 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 64 let addVC = AddContactViewController() 65 // 属性传值 66 let contact = DataHandle.shareInstence.findContactWithIndex(indexPath.row) 67 addVC.contact = contact 68 addVC.index = indexPath.row 69 addVC.myBlock = { 70 () -> Void in 71 self.tableView.reloadData() 72 } 73 self.navigationController?.pushViewController(addVC, animated: true) 74 } 75 76 // Override to support conditional editing of the table view. 77 // MARK: - 设置所有cell可编辑 78 override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 79 // Return false if you do not want the specified item to be editable. 80 return true 81 } 82 83 // Override to support editing the table view. 84 // MARK: - 提交编辑样式 85 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 86 if editingStyle == .Delete { 87 DataHandle.shareInstence.removeContactWithIndex(indexPath.row) 88 tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 89 } else if editingStyle == .Insert { 90 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 91 } 92 } 93 94 // Override to support conditional rearranging of the table view. 95 // MARK: - 设置所有cell可移动 96 override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 97 // Return false if you do not want the item to be re-orderable. 98 return true 99 } 100 101 // Override to support rearranging the table view. 102 // MARK: - 移动 103 override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 104 DataHandle.shareInstence.changeContact(fromIndexPath.row, toIndexPath: toIndexPath.row) 105 self.tableView.reloadData() 106 } 107 108 }
AddContactViewController.swift -- 添加联系人、修改联系人信息
1 import UIKit 2 3 class AddContactViewController: UIViewController { 4 5 // 声明控件属性 6 var nameTextField : UITextField? 7 var phoneTextField : UITextField? 8 var saveButton : UIButton? 9 // 属性传值 10 // 要修改的联系人信息 11 var contact : Contact? 12 // 要修改的联系人下标 13 var index : Int? 14 // 重定义,相当于typedef 15 typealias BLOCK = () -> Void 16 // 声明一个闭包属性 17 var myBlock : BLOCK? 18 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 self.view.backgroundColor = UIColor.whiteColor() 23 // 设置控件frame 24 self.nameTextField = UITextField(frame: CGRect(x: 30, y: 150, width: self.view.frame.size.width - 60, height: 30)) 25 self.phoneTextField = UITextField(frame: CGRect(x: CGRectGetMinX(self.nameTextField!.frame), y: CGRectGetMaxY(self.nameTextField!.frame) + 20, width: CGRectGetWidth(self.nameTextField!.frame), height: CGRectGetHeight(self.nameTextField!.frame))) 26 27 if self.contact != nil { 28 // 显示要修改的数据 29 self.nameTextField?.text = self.contact?.name 30 self.phoneTextField?.text = self.contact?.phone 31 } else { 32 // 添加 33 self.nameTextField?.placeholder = "请输入姓名" 34 self.phoneTextField?.placeholder = "请输入电话号码" 35 } 36 37 self.view.addSubview(self.nameTextField!) 38 self.view.addSubview(self.phoneTextField!) 39 40 self.saveButton = UIButton(type: UIButtonType.System) 41 self.saveButton?.frame = CGRect(x: CGRectGetMinX(self.phoneTextField!.frame), y: CGRectGetMaxY(self.phoneTextField!.frame) + 20, width: CGRectGetWidth(self.phoneTextField!.frame), height: CGRectGetHeight(self.phoneTextField!.frame)) 42 self.saveButton?.setTitle("保存", forState: UIControlState.Normal) 43 self.saveButton?.backgroundColor = UIColor.orangeColor() 44 self.saveButton?.addTarget(self, action: Selector("saveAction"), forControlEvents: UIControlEvents.TouchUpInside) 45 self.view.addSubview(self.saveButton!) 46 } 47 48 // MARK: - 保存按钮点击事件 49 func saveAction() { 50 // 点击保存按钮,将新的联系人添加到数组中 51 let newContact = Contact(name: self.nameTextField!.text!, phone: self.phoneTextField!.text!) 52 if self.contact != nil { 53 // 修改 54 DataHandle.shareInstence.updataContactWithIndex(newContact, index: self.index!) 55 } else { 56 // 添加 57 DataHandle.shareInstence.addContactToArray(newContact) 58 } 59 60 // 闭包传值(刷新UI) 61 self.myBlock!() 62 self.navigationController?.popViewControllerAnimated(true) 63 } 64 }
Contact.swift -- 联系人类
1 import UIKit 2 3 class Contact: NSObject { 4 // 声明属性 5 var name : String? 6 var phone : String? 7 8 // 初始化方法 9 init(name : String, phone : String) { 10 super.init() 11 self.name = name 12 self.phone = phone 13 } 14 }
DataHandle.swift -- 数据处理类
1 class DataHandle: NSObject { 2 var contactArray = Array<Contact>() 3 // 声明计算属性 4 var count : Int { 5 get { 6 return contactArray.count 7 } 8 } 9 10 static let shareInstence = DataHandle() 11 12 // 单例 13 // 为了防止在外部被再次初始化 14 private override init() { 15 super.init() 16 } 17 18 // MARK: - 添加联系人 19 func addContactToArray (contact : Contact) { 20 self.contactArray.append(contact) 21 } 22 23 // MARK: - 根据下标获取联系人 24 func findContactWithIndex (index : Int) -> Contact { 25 return self.contactArray[index] 26 } 27 28 // MARK: - 更新联系人信息 29 func updataContactWithIndex (newContact : Contact, index : Int) -> Void { 30 self.contactArray.removeAtIndex(index) 31 self.contactArray.insert(newContact, atIndex: index) 32 } 33 34 // MARK: - 删除联系人 35 func removeContactWithIndex (index : Int) -> Void { 36 self.contactArray.removeAtIndex(index) 37 } 38 39 // MARK: - 移动 40 func changeContact (fromIndexPath : Int, toIndexPath : Int) { 41 let contact = self.contactArray[fromIndexPath] 42 self.contactArray .removeAtIndex(fromIndexPath) 43 self.contactArray.insert(contact, atIndex: toIndexPath) 44 } 45 }
标签:
原文地址:http://www.cnblogs.com/fearlessyyp/p/5524528.html