- AppDelegate.swift
加个导航控制器
window?.rootViewController = UINavigationController(rootViewController: ViewController())
2.ViewContr0ller.swift
2.1懒加载BarButton
lazy var addBtn:UIBarButtonItem = {
let btn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "action")
return btn
}()
2.2懒加载TableView
lazy var tab:UITableView = {
let tv = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
tv.delegate = self
tv.dataSource = self
return tv
}()
2.3在ViewDidLoad中先添加几个联系人
for i in 0..<10
{
let contact = Contact(name: "Iphone\(i)", age: "9.\(i)", gender: "♀", phone: "110-911\(i)")
ContactManager.shareContactManager.addContact(contact)
}
2.4设置属性
view.backgroundColor = UIColor.whiteColor()
//注册cell
tab.registerNib(UINib(nibName: "ContactCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "ss")
view.addSubview(tab)
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addAction")
//实现方法
func addAction(){
let add = addViewController()
add.closure = {
()->()
in
self.tab.reloadData()
}
navigationController?.pushViewController(add, animated: true)
}
2.5将协议写在类的延展里
//extension 本类名:协议名{ }
extension ViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tab.dequeueReusableCellWithIdentifier("ss", forIndexPath: indexPath) as! ContactCell//强转
let contact = ContactManager.shareContactManager.contactArray[indexPath.row]
cell.cellWithContact(contact)
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ContactManager.shareContactManager.contactArray.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 110
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
// let detailVC = storyBoard.instantiateViewControllerWithIdentifier("") as! MyViewController
let vc = MyViewController()
let contact = ContactManager.shareContactManager.contactArray[indexPath.row]
vc.creat(contact)
navigationController?.pushViewController(vc, animated: true)
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete{
ContactManager.shareContactManager.removeContactByIndexPath(indexPath)
tab.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
}
}
3.添加页面addViewController.swift
3.1声明闭包和关联四个控件
var closure:(()->())?
@IBOutlet var nameLabel: UITextField!
@IBOutlet var genderLabel: UITextField!
@IBOutlet var ageLabel: UITextField!
@IBOutlet var phoneLabel: UITextField!
3.2实现添加联系人
@IBAction func BtnAction(sender: UIButton) {
let contact = Contact(name: nameLabel.text!, age: ageLabel.text!, gender: phoneLabel.text!, phone: phoneLabel.text!)
ContactManager.shareContactManager.contactArray.append(contact)
closure!()
navigationController?.popViewControllerAnimated(true)
}
4 自定义ContactCell.swift
@IBOutlet var nameLabel: UILabel!
@IBOutlet var ageLabel: UILabel!
@IBOutlet var genderLabel: UILabel!
@IBOutlet var phoneLabel: UILabel!
//根据contact给cell里面的标签赋值
func cellWithContact(contact:Contact){
nameLabel.text = contact.name
ageLabel.text = contact.age
genderLabel.text = contact.gender
phoneLabel.text = contact.phone
}
5.详情页面MyViewController.swift
//拖四个lab控件
@IBOutlet var nameLabel: UILabel!
@IBOutlet var genderLabel: UILabel!
@IBOutlet var ageLabel: UILabel!
@IBOutlet var phoneLabel: UILabel!
//声明四个属性
var name:String?
var age:String?
var gender:String?
var phone:String?
//定义个model函数方便传值
func creat(contact:Contact){
name = contact.name
age = contact.age
gender = contact.gender
phone = contact.phone
}
// 在ViewDidload里面进行赋值
nameLabel.text = name
genderLabel.text = gender
ageLabel.text = age
phoneLabel.text = phone
6.创建单例类ContactManager.swift
6.1 将联系人管理类声明为单例类,管理所有联系人的操作,增删改查
static let shareContactManager:ContactManager = {
let contactManager = ContactManager()
return contactManager
}()
6.2 所有联系人的数组
var contactArray:[Contact] = [Contact]()
func addContact(contact:Contact){
contactArray.append(contact)
}
6.3 移除联系人
func removeContactByIndexPath(indexpath:NSIndexPath){
contactArray.removeAtIndex(indexpath.row)
}