swift中cell的两种创建区别和extension的使用。让我们开发更有条理
2015-10-12 09:03
627 查看
//
// AppDelegate.swift
// WeiBo
//
// Created by 胡双飞 on 15/10/4.
// Copyright © 2015年 HSF. All rights reserved.
//
import UIKit
//MARK:定义cell注册问题
private let HomeCellIdentifier = "HomeCell"
class HomeTableViewController: BaseTableViewController {
override func viewDidLoad() {
super.viewDidLoad()
if !UserAccountViewModel.shareUserAccount.isUserLogin{
visitorView?.setupInfo(nil, message: "关注一些人,回这里看看有什么惊喜")
}
loadData()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: HomeCellIdentifier)
}
//加载数据
private func loadData(){
statusListViewModel.loadStatus().subscribeError({ (error) -> Void in
printLog(error)
}) { () -> Void in
self.tableView.reloadData()
}
}
//MARK: - 懒加载
private lazy var statusListViewModel = StatusListViewModel()
}
//extension 类似于OC中的分类,扩展的意思。我们可以将UITableViewDataSource/UITabelTableViewDelegate放到这里更加清晰
extension HomeTableViewController{
//MARK:UITableViewController
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return statusListViewModel.status?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//1 iOS5.0时 dequeueReusableCellWithIdentifier: 会查询可重用cell,如果有则拿来用,如果没有则创建一个。
//2 iOS6.0时 dequeueReusableCellWithIdentifier: forIndexPath:需要registerCell/storyboard/xib注册一个cell,,如果缓存区cell不存在,会使用cell实利化一个新的cell。
let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellIdentifier, forIndexPath: indexPath)
//1.获取微博数据
let status = statusListViewModel.status![indexPath.item] as! Status
cell.textLabel?.text = status.text
return cell
}
}
// AppDelegate.swift
//
// Created by 胡双飞 on 15/10/4.
// Copyright © 2015年 HSF. All rights reserved.
//
import UIKit
//MARK:定义cell注册问题
private let HomeCellIdentifier = "HomeCell"
class HomeTableViewController: BaseTableViewController {
override func viewDidLoad() {
super.viewDidLoad()
if !UserAccountViewModel.shareUserAccount.isUserLogin{
visitorView?.setupInfo(nil, message: "关注一些人,回这里看看有什么惊喜")
}
loadData()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: HomeCellIdentifier)
}
//加载数据
private func loadData(){
statusListViewModel.loadStatus().subscribeError({ (error) -> Void in
printLog(error)
}) { () -> Void in
self.tableView.reloadData()
}
}
//MARK: - 懒加载
private lazy var statusListViewModel = StatusListViewModel()
}
//extension 类似于OC中的分类,扩展的意思。我们可以将UITableViewDataSource/UITabelTableViewDelegate放到这里更加清晰
extension HomeTableViewController{
//MARK:UITableViewController
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return statusListViewModel.status?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//1 iOS5.0时 dequeueReusableCellWithIdentifier: 会查询可重用cell,如果有则拿来用,如果没有则创建一个。
//2 iOS6.0时 dequeueReusableCellWithIdentifier: forIndexPath:需要registerCell/storyboard/xib注册一个cell,,如果缓存区cell不存在,会使用cell实利化一个新的cell。
let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellIdentifier, forIndexPath: indexPath)
//1.获取微博数据
let status = statusListViewModel.status![indexPath.item] as! Status
cell.textLabel?.text = status.text
return cell
}
}
相关文章推荐
- Swift之贪婪的UIButton
- swift单例和oc单例
- swift中防止闭包循环引用
- 在swift中 @objc 、private 和事件问题
- swift中方便调试的日志输出
- coredata swift 简单入门
- 如果使用swift,先不用pod
- swift第一个HelloWord实现
- Swift学习之UI开发初探
- 16.Swift 播放视频
- 15.Swift 录制音频
- swift is only available on iOS 9.0 or newer 报错
- [ios][swift]swift中如果做基本类型的转换
- Swift 解决dismissViewControllerAnimated关闭当前Controller无效的问题
- 类方法的使用
- model的使用
- tableview使用自定义类,页面跳转,本地存储
- swift中tableview的使用
- swift3.0 Missing argument label 'xx:' in call
- swift中字符串截取方法(substring)