IOS:Swift-自定义cell的混用-简易通讯录的传值

AppDelegate.swift:

 self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
        self.window?.makeKeyAndVisible()
        //创建导航视图控制器的根视图
        let vc = TongXLTableViewController()
        //2.创建导航视图控制器,并为她制定根视图控制器
        let navigation = UINavigationController(rootViewController: vc)
        
        //3.将导航视图控制器设置为window的根视图控制器
        self.window?.rootViewController = navigation

TongXunLu.swift:

class TongXunLu: NSObject {
    var name:String!
    var phone:String!
    var age:String!
    var email:String!
    override init() {
        super.init()
    }
    init(name:String,phone:String,age:String,email:String) {
        self.name = name
        self.phone = phone
        self.age = age
        self.email = email
    }
}

SecondViewController.swift:

//
//  SecondViewController.swift
//  practice tongxunlu
//
//  Created by 任师艳 on 2016/11/30.
//  Copyright © 2016年 任师艳. All rights reserved.
//

import UIKit

class SecondViewController: UIViewController {

    //属性传值第一步
    var content:TongXunLu!
     var name:UILabel!
     var phone:UILabel!
     var age:UILabel!
     var email:UILabel!
    
    var nametextfield:UITextField!
    var phonetextfield:UITextField!
    var agetextfield:UITextField!
    var emailtextfield:UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpData()
        // Do any additional setup after loading the view.
        //属性传值第三部
        self.nametextfield.text = content.name
      self.phonetextfield.text = content.phone
       self.agetextfield.text = content.age
       self.emailtextfield.text = content.email
        
    }

    func setUpData()   {
        name = UILabel(frame: CGRect(x: 100, y: 100, width: 80, height: 50))
        name.text = "姓名"
        name.backgroundColor = UIColor.red
        self.view.addSubview(name)
        
        nametextfield = UITextField(frame: CGRect(x: 200, y: 100, width: 100, height: 50))
        nametextfield.backgroundColor = UIColor.yellow
        self.view.addSubview(nametextfield)
        
        phone = UILabel(frame: CGRect(x: 100, y: 180, width: 80, height: 50))
        phone.backgroundColor = UIColor.red
        phone.text = "电话"
        self.view.addSubview(phone)
        
        phonetextfield = UITextField(frame: CGRect(x: 200, y: 180, width: 100, height: 50))
        phonetextfield.backgroundColor = UIColor.yellow
        self.view.addSubview(phonetextfield)
        
        age = UILabel(frame: CGRect(x: 100, y: 260, width: 80, height: 50))
        age.backgroundColor = UIColor.red
        age.text = "年龄"
        self.view.addSubview(age)
        
        agetextfield = UITextField(frame: CGRect(x: 200, y: 260, width: 100, height: 50))
        agetextfield.backgroundColor = UIColor.yellow
        self.view.addSubview(agetextfield)
        
        email = UILabel(frame: CGRect(x: 100, y: 340, width: 80, height: 50))
        email.text = "邮箱"
        email.backgroundColor = UIColor.red
        self.view.addSubview(email)
        
        emailtextfield = UITextField(frame: CGRect(x: 200, y: 340, width: 100, height: 50))
        emailtextfield.backgroundColor = UIColor.yellow
        self.view.addSubview(emailtextfield)
        
    }

    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    
    }
    }

LianxirenCell.swift:

 private var name:UILabel!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
      self.setUpData()
       
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //封装赋值的方法->接口
    func setvalueByArray(txl:TongXunLu)   {
        name.text = txl.name
    }
    
    func setUpData()   {
        name = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
        self.contentView.addSubview(name)
        
    }
    

TongXLTableViewController.swift:

class TongXLTableViewController: UITableViewController {

    let identifier = "cell"
    var TXLArray:[TongXunLu] = Array()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.readDataFromArray()
        self.tableView.register(LianxirenCell.self, forCellReuseIdentifier: identifier)
    }
    func readDataFromArray()   {
        let array = [["name":"阿拉蕾","phone":"12222222","age":"3","email":"[email protected]"],["name":"阿宝","phone":"13333333","age":"42","email":"[email protected]"],
            ["name":"宋仲基","phone":"18888888","age":"22","email":"@678qq.com"],
            ["name":"宋民国","phone":"14569870","age":"12","email":"[email protected]"],
            ["name":"小鱼儿","phone":"123456789","age":"3","email":"[email protected]"],
            ["name":"小燕子","phone":"1456987","age":"23","email":"[email protected]"],
            ["name":"吴亦凡","phone":"169875321","age":"18","email":"[email protected]"],
            ["name":"彭于晏","phone":"789654321","age":"18","email":"[email protected]"]
        ]
        for a in array{
        let stu = TongXunLu()
            stu.name = a["name"]
            stu.phone = a["phone"]
            stu.age = a["age"]
            stu.email = a["email"]
            //将对象添加到数组
            TXLArray.append(stu)
        }
        
    }

override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return TXLArray.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! LianxirenCell
        cell.setvalueByArray(txl: TXLArray[indexPath.row])
        

        return cell
    }
   

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let secondVC = SecondViewController()
        //属性传值第二步
        secondVC.content = TXLArray[indexPath.row]
        
        self.navigationController?.pushViewController(secondVC, animated: true)
    }

你可能感兴趣的:(IOS:Swift-自定义cell的混用-简易通讯录的传值)