學(xué)習(xí)筆記---Swift通訊錄

  1. AppDelegate.swift

加個(gè)導(dǎo)航控制器

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中先添加幾個(gè)聯(lián)系人

 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設(shè)置屬性

        view.backgroundColor = UIColor.whiteColor()
//注冊(cè)cell
        tab.registerNib(UINib(nibName: "ContactCell", bundle:   NSBundle.mainBundle()), forCellReuseIdentifier: "ss")
        view.addSubview(tab)        
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addAction")
//實(shí)現(xiàn)方法
  func addAction(){
        let add = addViewController()
        add.closure = {
            ()->()
            in
            self.tab.reloadData()
            }
      navigationController?.pushViewController(add, animated: true)
    }

2.5將協(xié)議寫在類的延展里

//extension 本類名:協(xié)議名{ }
extension ViewController:UITableViewDelegate,UITableViewDataSource{    
         func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->  UITableViewCell {
        let cell = tab.dequeueReusableCellWithIdentifier("ss", forIndexPath: indexPath) as! ContactCell//強(qiáng)轉(zhuǎn)
         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.添加頁(yè)面addViewController.swift

3.1聲明閉包和關(guān)聯(lián)四個(gè)控件

    var closure:(()->())?
    @IBOutlet var nameLabel: UITextField!  
    @IBOutlet var genderLabel: UITextField!
    @IBOutlet var ageLabel: UITextField!  
    @IBOutlet var phoneLabel: UITextField!

3.2實(shí)現(xiàn)添加聯(lián)系人

@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!
    //根據(jù)contact給cell里面的標(biāo)簽賦值
    func cellWithContact(contact:Contact){
        nameLabel.text = contact.name
        ageLabel.text = contact.age
        genderLabel.text = contact.gender
        phoneLabel.text = contact.phone
    }

5.詳情頁(yè)面MyViewController.swift

//拖四個(gè)lab控件
    @IBOutlet var nameLabel: UILabel!    
    @IBOutlet var genderLabel: UILabel!
    @IBOutlet var ageLabel: UILabel!
    @IBOutlet var phoneLabel: UILabel!
//聲明四個(gè)屬性
    var name:String?
    var age:String?
    var gender:String?
    var phone:String?
//定義個(gè)model函數(shù)方便傳值
    func creat(contact:Contact){
        name = contact.name
        age = contact.age
        gender = contact.gender
        phone = contact.phone
}
//  在ViewDidload里面進(jìn)行賦值
        nameLabel.text = name
        genderLabel.text = gender
        ageLabel.text = age
        phoneLabel.text = phone

6.創(chuàng)建單例類ContactManager.swift

6.1 將聯(lián)系人管理類聲明為單例類,管理所有聯(lián)系人的操作,增刪改查

   static let shareContactManager:ContactManager = {
        let contactManager = ContactManager()
        return contactManager
    }()

6.2 所有聯(lián)系人的數(shù)組

    var contactArray:[Contact] = [Contact]()
    func addContact(contact:Contact){
          contactArray.append(contact)
    }

6.3 移除聯(lián)系人

    func removeContactByIndexPath(indexpath:NSIndexPath){
        contactArray.removeAtIndex(indexpath.row)
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容