通讯录

作者: 抖腿宅还爱古风 | 来源:发表于2016-06-14 20:34 被阅读0次

    !!!!! 大手们请多多指教
    今天试写了一下通讯录,有增加和删除功能...逻辑上大致和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()
        }
    ````

    相关文章

      网友评论

        本文标题:通讯录

        本文链接:https://www.haomeiwen.com/subject/nspudttx.html