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: "456@qq.com")
    contactSource["s"] = [s1,s2]
    let x1 = contacter(name: "小鱼儿", phone: "14758963587", age: 15, email: "999@168.com")
    let x2 = contacter(name: "小燕子", phone: "18388767138", age: 18, email: "987@qq.com")
    contactSource["x"] = [x1,x2]
    let a1 = contacter(name: "阿拉蕾", phone: "53947962889", age: 18, email: "489@qq.com")
    let a2 = contacter(name: "阿宝", phone: "78965241958", age: 27, email:"123@qq.com")
    contactSource["a"] = [a1,a2]
    let w1 = contacter(name: "吴亦凡", phone: "15789654862", age: 25, email: "@666qq.com")
    let w2 = contacter(name: "无良", phone: "17589632144", age: 20, email: "888@qq.com")
    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: "889@qq.com")
                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)
        
    }


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,165评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,503评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,295评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,589评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,439评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,342评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,749评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,397评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,700评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,740评论 2 313
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,523评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,364评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,755评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,024评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,297评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,721评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,918评论 2 336

推荐阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,465评论 1 14
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,952评论 4 60
  • 一开始, 你们就像, 企鹅与北极熊 后来, 中间隔着, 银河系。
    温暖的白熊阅读 230评论 0 0
  • vimgrep /匹配模式/[g][j] 要搜索的文件/范围g:表示是否把每一行的多个匹配结果都加入j:表示是否搜...
    a_pioneer阅读 455评论 0 0