IOS:Swift-UITableView的练习与通讯录传值

AppDelegate.swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
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 = dataTableViewController()
//2.创建导航视图控制器,并为她制定根视图控制器
let navigation = UINavigationController(rootViewController: vc)

    //3.将导航视图控制器设置为window的根视图控制器
    self.window?.rootViewController = navigation

    
    
    return true
}

dataTableViewController.swift:
class dataTableViewController: UITableViewController {

var contactSource:[String:[contacter]] = Dictionary()

var keysArray:[String]?
let identifier = "cell"

override func viewDidLoad() {
    super.viewDidLoad()

    self.setupdic()
    //取出字典contactsource中的key
    let keys = self.contactSource.keys
    //,排序后赋值给keyArray
    keysArray = keys.sorted()
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
    self.navigationItem.rightBarButtonItem = self.editButtonItem

    
}

func setupdic() {
    
    
    let s1=contacter(name: "宋仲基", phone: "18347457138", age: 28, email: "166@qq,com")
    let s2 = contacter(name: "宋民国", phone: "15764741110", age: 18, email: "[email protected]")
    contactSource["s"] = [s1,s2]
    let x1 = contacter(name: "小鱼儿", phone: "14758963587", age: 15, email: "[email protected]")
    let x2 = contacter(name: "小燕子", phone: "18388767138", age: 18, email: "[email protected]")
    contactSource["x"] = [x1,x2]
    let a1 = contacter(name: "阿拉蕾", phone: "53947962889", age: 18, email: "[email protected]")
    let a2 = contacter(name: "阿宝", phone: "78965241958", age: 27, email:"[email protected]")
    contactSource["a"] = [a1,a2]
    let w1 = contacter(name: "吴亦凡", phone: "15789654862", age: 25, email: "@666qq.com")
    let w2 = contacter(name: "无良", phone: "17589632144", age: 20, email: "[email protected]")
    contactSource["w"] = [w1,w2]
    
}
方法:
 override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return contactSource.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        let key = keysArray?[section]
        let group = contactSource[key!]
        
        return (group?.count)!
    }

    /**/
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
 
        let key = keysArray?[indexPath.section]
        let group = contactSource[key!]
        let c = group?[indexPath.row]
        cell.textLabel?.text = c?.name

        return cell
    }
    //header
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return keysArray?[section]
    }
    //footer
    override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        return keysArray?[section]
    }
    //right
    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return keysArray
    }
    

    /*2.设置哪些cell可以编辑*/
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        if indexPath.section < 2 {
        return .delete
        }else
        {
        return .insert
        }
    }

    /* */
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        
            let key = keysArray?[indexPath.section]
            var group = contactSource[key!]
            if editingStyle == .delete {
                if group?.count == 1{
                contactSource.removeValue(forKey: key!)
                    keysArray?.remove(at: indexPath.section)
                    let set = NSIndexSet(index: indexPath.section)
                    tableView.deleteSections(set as IndexSet, with: .left)
                }else{
                group?.remove(at: indexPath.row)
                    contactSource[key!] = group
                    tableView.deleteRows(at: [indexPath], with: .fade)

                }

        } else if editingStyle == .insert {
            let name = contacter(name: "向日葵", phone: "99999", age: 22, email: "[email protected]")
                group?.append(name)
                contactSource[key!] = group
                tableView.insertRows(at: [indexPath], with: .right)
                tableView.reloadData()
        }
    }
    
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }

   

    /**/
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        let key = keysArray?[fromIndexPath.section]
        var group = contactSource[key!]
        let a = group?[fromIndexPath.row]
        group?.remove(at: fromIndexPath.row)
        group?.insert(a!, at: to.row)
        contactSource[key!] = group

    }
    override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
        if sourceIndexPath.section == proposedDestinationIndexPath.section{
        return proposedDestinationIndexPath
        }else{
        return sourceIndexPath
        }
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let secondVC = SecondViewController()
        let key = keysArray?[indexPath.section]
        //根据key取出字典中的value。数组
        let group = contactSource[key!]
        //根据cell的下标取出数组对应位置的元素
        let c = group?[indexPath.row]
         secondVC.content1 = c?.name
        secondVC.content2 = c?.phone
        secondVC.content3 = c?.age
        secondVC.content4 = c?.email
        self.navigationController?.pushViewController(secondVC, animated: true)
    }

SecondViewController.swift:

class SecondViewController: UIViewController {

    var label1:UILabel!
    var label2:UILabel!
    var label3:UILabel!
    var label4:UILabel!
    
    var textfield1:UITextField!
    var textfield2:UITextField!
    var textField3:UITextField!
    var textfield4:UITextField!
    
    //
    var content1:String!
    var content2:String!
    var content3:Int!
    var content4:String!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.cyan
        self.setup()
        self.textfield1.text = self.content1
        self.textfield2.text = self.content2
        self.textField3.text = String( self.content3)
        self.textfield4.text = self.content4
    }
    func setup()  {
        label1 = UILabel(frame: CGRect(x: 100, y: 100, width: 80, height: 50))
        label1.text = "姓名"
        label1.backgroundColor = UIColor.red
        self.view.addSubview(label1)
        
        textfield1 = UITextField(frame: CGRect(x: 200, y: 100, width: 200, height: 50))
        textfield1.backgroundColor = UIColor.yellow
        self.view.addSubview(textfield1)
        
        label2 = UILabel(frame: CGRect(x: 100, y: 200, width: 80, height: 50))
        label2.backgroundColor = UIColor.red
        label2.text = "电话"
        self.view.addSubview(label2)
        
        textfield2 = UITextField(frame: CGRect(x: 200, y: 200, width: 200, height: 50))
        textfield2.backgroundColor = UIColor.yellow
        self.view.addSubview(textfield2)
        
        label3 = UILabel(frame: CGRect(x: 100, y: 300, width: 80, height: 50))
        label3.text = "年龄"
        label3.backgroundColor = UIColor.red
        self.view.addSubview(label3)
        
        textField3 = UITextField(frame: CGRect(x: 200, y: 300, width: 200, height: 50))
        textField3.backgroundColor = UIColor.yellow
        self.view.addSubview(textField3)
        
        label4 = UILabel(frame: CGRect(x: 100, y: 400, width: 80, height: 50))
        label4.backgroundColor = UIColor.red
        label4.text = "邮箱"
        self.view.addSubview(label4)
        
        textfield4 = UITextField(frame: CGRect(x: 200, y: 400, width: 200, height: 50))
        textfield4.backgroundColor = UIColor.yellow
        self.view.addSubview(textfield4)
        
    }


你可能感兴趣的:(IOS:Swift-UITableView的练习与通讯录传值)