本人有若干成套学习视频, 可试看! 可试看! 可试看, 重要的事情说三遍 包含Java
, 数据结构与算法
, iOS
, 安卓
, python
, flutter
等等, 如有需要, 联系微信tsaievan
.
- 联系人demo的总体思路:
-
各个类的主要功能
Snip20160905_3.png -
具体的代码实现
-AppDelegate中的代码实现
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
}
- ListViewController中的代码实现
-属性的设置
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
}
}
- Person模型类,用来获取网络数据
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)
})
}
}
}
- 自定义cell,在cell中获取people数据,然后赋值给cell的各个控件
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
}
}
}
- 实现效果
-点击cell后
Snip20160905_5.png
网友评论