联系人demo的实现(Swift)

2016-09-05  本文已影响0人  小冰山口

本人有若干成套学习视频, 可试看! 可试看! 可试看, 重要的事情说三遍 包含Java, 数据结构与算法, iOS, 安卓, python, flutter等等, 如有需要, 联系微信tsaievan.

Snip20160905_1.png
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let listViewController = ListViewController()
        window?.rootViewController = UINavigationController(rootViewController: listViewController)
        window?.makeKeyAndVisible()
        return true
    }
import UIKit

let identifier = "cell"
class ListViewController: UIViewController {
    
    lazy var peopleArray:[Person] = []
    
    lazy var tableView:UITableView = {
        let tableView = UITableView()
        
        // 设置tableView的代理和数据源
        tableView.delegate = self
        tableView.dataSource = self
        
        //利用Person类的类方法,拿到网络数据,并赋值给peopleArray这个可变数组
        Person.peopleInfos{
            PersonArray ->Void in
            
            self.peopleArray = PersonArray
            tableView.reloadData()
        }
        return tableView
    }()

-视图的生命周期

    // MARK:- 视图的生命周期
    // MARK:- 重写loadView方法,将tableView设置为控制器的view
    override func loadView() {
        self.view = tableView
    }

    override func viewDidLoad() {
        
        super.viewDidLoad()
        title  = "联系人"
        // tableView注册类,这样返回cell的方法才可以用,不然就会崩掉
        tableView.registerClass(PeopleCell.self, forCellReuseIdentifier: identifier)
    }
    
}

-数据源和代理协议方法的实现(用类延展来实现)

extension ListViewController: UITableViewDelegate {
    
    // MARK:- 点击cell跳转详情页控制器
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let viewController = DetailViewController()
        viewController.people = self.peopleArray[indexPath.row]
        self.navigationController?.pushViewController(viewController, animated: true)
    }
    
}


extension ListViewController:UITableViewDataSource {
    
    // MARK:- 返回行数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.peopleArray.count
    }
    
    // MARK:- 返回cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! PeopleCell
        
        cell.people = self.peopleArray[indexPath.row]
        
        return cell
    }
}
import UIKit

class Person: NSObject {
    var name:String?
    var phone:String?
    // MARK:- KVC构造函数
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    // MARK:- 提供一个类方法,获取网络数据,然后完成闭包的回调
    class func peopleInfos(callBack:[Person] ->Void){
        dispatch_async(dispatch_get_global_queue(0, 0)) {
            let peopleInfos:[[String:AnyObject]] = [
                ["name":"jack0","phone":"a12345"],
                ["name":"jack1","phone":"b12345"],
                ["name":"jack2","phone":"c12345"],
                ["name":"jack3","phone":"d12345"],
                ["name":"jack4","phone":"e12345"],
                ["name":"jack5","phone":"f12345"],
                ["name":"jack6","phone":"g12345"],
                ["name":"jack7","phone":"h12345"],
            ]
            var peopleArray:[Person] = []
            for peopleInfo:[String:AnyObject] in peopleInfos
            {
                let people = Person(dict: peopleInfo)
                peopleArray.append(people)
            }
            dispatch_async(dispatch_get_main_queue(), {
                callBack(peopleArray)
            })
        }
    }
}

import UIKit

class PeopleCell: UITableViewCell {
    // MARK:- 重写init(style: UITableViewCellStyle, reuseIdentifier: String?)方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var people:Person? {
        didSet{
            self.textLabel?.text = people?.name
            self.detailTextLabel?.text = people?.phone
        }
    }
}
Snip20160905_4.png

-点击cell后

Snip20160905_5.png

PS. 本人有若干成套学习视频, 包含Java, 数据结构与算法, iOS, 安卓, python, flutter等等, 如有需要, 联系微信tsaievan.

上一篇下一篇

猜你喜欢

热点阅读