通訊錄 <swift>

!!!!! 大手們請多多指教
今天試寫了一下通訊錄,有增加和刪除功能...邏輯上大致和OC相同,只是在語法上稍作改變。順序有些亂多包涵!!

首先先創建好會使用到得類:model類、工具類(用來管理一系列的操作)

// model類里面聲明兩個屬性
var name:String?
var number:String?

// 重寫初始化的方法
init(name:String,number:String) 
{
   super.init()
   self.name = name
   self.number = number
}
// 將工具類寫成一個單例
static let shareInstance:ModelTool = 
{
   let modeltool = ModelTool()
   return modeltool
}()

// 定義一個數組用來存放聯系人
var modelArray:[Model] = [Model]()

加載一個tableView做主界面,有導航欄和barBtn .barBtn實現點擊跳轉到 add頁面中

lazy var tableView:UITableView = {
  let tableView1 = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
  tableView1.delegate = self
  tableView1.dataSource = self
  return tableView1 
}()
// 加載 添加按鈕
lazy var leftBtn:UIBarButtonItem = {
        let leftBtn1 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addAction:")
        return leftBtn1
    }()
// 添加方法
func addAction(btn:UIBarButtonItem)
    {
        let AddVC = AddViewController()
        editBtn.title = "編輯"
        tableView.editing = false
        navigationController?.pushViewController(AddVC, animated: true)
    }

// 加載編輯按鈕
lazy var editBtn:UIBarButtonItem = {
        let editBtn1 = UIBarButtonItem(title: "編輯", style: UIBarButtonItemStyle.Plain, target: self, action: "editAction:")
        return editBtn1
    }()

// 編輯按鈕的兩個狀態下得不同功能
 func editAction(btn:UIBarButtonItem)
    {
        if (editBtn.title == "取消")
        {
            editBtn.title = "編輯"
            tableView.editing = false
        }
        else
        {
            editBtn.title = "取消"
            tableView.editing = true
        }
    }
 // 將按鈕添加到導航欄中
  navigationItem.leftBarButtonItem = leftBtn
  navigationItem.rightBarButtonItem = editBtn
````
因為要遵守協議,并且協議里面有必須實現的方法。直接寫一個延展用來實現協議方法,寫在 class外層
````
extension  ViewController:UITableViewDelegate,UITableViewDataSource{
// 返回cell,并且賦值. !!!不要忘記注冊(我省略了)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{
   // as! 是為了強制轉化
   let cell = tableView.dequeueReusableCellWithIdentifier("xx", forIndexPath: indexPath) as! ModelTableViewCell
   let  model = ModelTool.shareInstance.modelArray[indexPath.row]
   cell.modelConfigure(model)
   return cell

}
// 返回分區
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{
     return  ModelTool.shareInstance.modelArray.count
}

// 刪除聯系人
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{ 
// 先操作數據源 再操作 UI
   ModelTool.shareInstance.modelArray.removeAtIndex(index.row)
   tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Right)
}


}
````
自定義 cell,在里面加載相應的控件。我就不寫這些控件代碼了,直接寫給控件賦值的代碼
````
func modelConfigure(model:Model)
{
   nameL.text = model.name
   numberL.text = model.number
}
````
現在是添加頁面的代碼,控件代碼依舊不寫(兩個TextField,兩個Lable),在添加頁面中有一個add的btn。這里只寫btn的方法
````
 func addAction(btn:UIButton) {
        if (nameTF.text?.characters.count == 0)
        {
            return
        }
   // 添加一個 model,model的具體數據
        let model = Model(name: self.nameTF.text!,number: self.numberTF.text!)
   //  添加到model數組中
        ModelTool.shareInstance.modelArray.append(model)
        navigationController?.popViewControllerAnimated(true)
    }

````

最后在主頁面將要顯示的時候刷新UI
````
override func viewWillAppear(animated: Bool) {
        tableView.reloadData()
    }
````
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容