影单
import UIKit
class FilmListModel: NSObject,YYModel {
var follow_count = ""
var movie_count = ""
var name = ""
var pagelist_id = ""
}
class FilmListLineModel: NSObject,YYModel {
var can_wanttosee = ""
var film_id = ""
var is_wanttosee = ""
//名字
var name = ""
var play_js = ""
//图片地址
var poster_url = ""
//评分
var score = ""
var user_score = ""
var video_url = ""
var wanttosee = ""
}
import UIKit
import Alamofire
class FilmListTableViewCell: UITableViewCell {
//MARK: - 属性
//1.子控件
@IBOutlet weak var colorView: UIView!
@IBOutlet weak var checkAllButton: UIButton!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
//3.cell上的collectionView对应的数据源数组
lazy var dataArray:NSMutableArray = {
return NSMutableArray()
}()
//2.显示的数据对应的模型
var model:FilmListModel? = nil{
didSet{
self.checkAllButton.setTitle("查看全部\(model!.movie_count)部", forState: .Normal)
self.nameLabel.text = model?.name
if self.dataArray.count == 0 {
//根据id请求数据
self.getNetData(model!.pagelist_id)
}else{
self.collectionView.reloadData()
}
}
}
//4.将请求结果传到视图控制器中
var sendData:((String,NSArray)->Void)? = nil
}
//MARK: - 界面设置
extension FilmListTableViewCell{
//当cell从xib上加载成功后会自动调用的方法
override func awakeFromNib() {
super.awakeFromNib()
//1.注册cell
self.collectionView.registerNib(UINib.init(nibName: "FilmListLineCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
}
}
//MARK: - collectionView 协议方法
extension FilmListTableViewCell:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
//cell的个数
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.dataArray.count
}
//创建cell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! FilmListLineCollectionViewCell
cell.model = self.dataArray[indexPath.row] as? FilmListLineModel
return cell
}
//设置cell的大小
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
let H = collectionView.frame.size.height
let W:CGFloat = 100
return CGSizeMake(W, H)
}
//边距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
return UIEdgeInsetsMake(5, 5, 5, 5)
}
}
//MARK: - 获取网络数据
extension FilmListTableViewCell{
func getNetData(id:String){
Alamofire.request(.POST, Url_FilmListLine, parameters: ["count":10,"page":1,"type":"recommend","id":id], encoding: .URL, headers: nil).responseJSON(options: .MutableContainers) { (data) in
//解析数据
if let json = data.result.value{
//获取字典数组
let dataDict = json.objectForKey("data") as! NSDictionary
let listArray = dataDict["list"]
//将字典数组转换成模型数组
let modelArray = NSArray.yy_modelArrayWithClass(FilmListLineModel.self, json: listArray)
//将数据源数组中的数据全部移除
self.dataArray.removeAllObjects()
//将模型数组的数据放到数据源数组中
self.dataArray.addObjectsFromArray(modelArray)
//将模型数组传到视图控制器中去做缓存
self.sendData!(id,modelArray)
//刷新collectionView
self.collectionView.reloadData()
}
}//请求方法结束
}//函数结束
}
import UIKit
import Kingfisher
class FilmListLineCollectionViewCell: UICollectionViewCell {
//子控件
@IBOutlet weak var coverImageView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var starView: StarView!
//模型
var model:FilmListLineModel? = nil{
didSet{
//图片
coverImageView.kf_setImageWithURL(NSURL.init(string: model!.poster_url)!, placeholderImage: UIImage.init(named: "movie_default_dark_300x450"))
//名字
nameLabel.text = model?.name
//评分
scoreLabel.text = model?.score
//显示星级
let ocStr = NSString.init(string: (model?.score)!)
starView.score = ocStr.floatValue
}
}
}
class StarView: UIView {
//MARK: - 属性
//1.分数(满分是10)
var score:Float = 0{
//给分数赋值的时候设置星星的图片显示
didSet{
self.showStarImage(score)
}
}
//MARK: - 构造方法
//通过代码创建当前类的对象的时候调用
// override init(frame: CGRect) {
// super.init(frame: frame)
//
// //在创建当前类的对象的时候,将5颗星对应的imageView创建出来
// for _ in 0...4 {
//
// let imageView = UIImageView.init()
// self.addSubview(imageView)
// }
//
// }
//
// required init?(coder aDecoder: NSCoder) {
// fatalError("init(coder:) has not been implemented")
// }
//
//通过xib创建当前类的对象的时候会调用
override func awakeFromNib() {
super.awakeFromNib()
//在创建当前类的对象的时候,将5颗星对应的imageView创建出来
for _ in 0...4 {
let imageView = UIImageView.init()
self.addSubview(imageView)
// imageView.image = UIImage.init(named: "rating_smallstar_selected_dark")
}
}
}
//MARK: - 根据分数设置星星的显示
extension StarView{
func showStarImage(score:Float){
//1.获取分数对应的星级
let realStarScore = score/2
//2.创建星星图片对象
let starImage1 = UIImage.init(named: "rating_smallstar_selected_dark")
let starImage2 = UIImage.init(named: "rating_smallstar_half_light")
let starImage3 = UIImage.init(named: "rating_smallstar_unchecked_light")
//3.获取星级的整数部分
let intStarScore = Int(realStarScore)
//4.获取星级的小数部分
let floatStarScore = realStarScore - Float(intStarScore)
//遍历拿到所有的imageView
for (i,item) in self.subviews.enumerate() {
let imageView = item as! UIImageView
//如果小于整数部分肯定是满星
if i<intStarScore {
imageView.image = starImage1
}
//如果大于整数部分肯定是空星
if i>intStarScore {
imageView.image = starImage3
}
//如果等于整数部分可能是半星也可能是空星
if i==intStarScore {
if floatStarScore>=0.5 {
imageView.image = starImage2
}else{
imageView.image = starImage3
}
}
}
}
}
//MARK: - 计算每颗星的frame
extension StarView{
override func layoutSubviews() {
super.layoutSubviews()
//间距
let margin:CGFloat = 2
//星星的宽度
let starW = (self.frame.size.width - 6*margin)/5
//星星的高度
let starH = starW
//y坐标
let starY = (self.frame.size.height - starH)/2
//遍历所有的imageView,设置frame
for (i,item) in self.subviews.enumerate() {
let x = (starW + margin)*CGFloat(i) + margin
item.frame = CGRectMake(x,starY, starW, starH)
}
}
}
import UIKit
import Alamofire
class FilmListViewController: BasicViewController {
//MARK: - 属性
//1.数据源数组
lazy var dataArray:NSMutableArray = {
return NSMutableArray()
}()
//2.tableView
var tableView:UITableView!
//3.缓存当前界面上的分组的电影数据
lazy var filmDataCache:NSCache = {
return NSCache()
}()
}
//MARK: - 生命周期
extension FilmListViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBarItemSetting()
self.getNetData()
}
}
//MARK: - 获取网路数据
extension FilmListViewController{
func getNetData() {
Alamofire.request(.GET, Url_FilmList, parameters: ["count":10,"page":self.dataArray.count/10], encoding: .URL, headers: nil).responseJSON(options: .MutableContainers) { (data) in
//解析数据
if let json = data.result.value{
//获取字典数组
let dataDict = json.objectForKey("data") as! NSDictionary
let listArray = dataDict["list"]
//将字典数组转换成模型数组
let modelArray = NSArray.yy_modelArrayWithClass(FilmListModel.self, json: listArray)
//将模型数组中的数组添加到数据源数组中
self.dataArray.addObjectsFromArray(modelArray)
//刷新tableView
self.tableView.reloadData()
}else{
KVNProgress.showErrorWithStatus("请求失败")
}
}//请求方法结束
}//函数结束
}
//MARK: - 界面相关
extension FilmListViewController{
override func navigationBarItemSetting() {
super.navigationBarItemSetting()
//1.设置navigationItem
self.navigationItem.title = "影单"
//2.添加tableView
self.tableView = UITableView.init(frame: self.view.bounds, style: .Grouped)
self.view.addSubview(self.tableView)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.registerNib(UINib.init(nibName: "FilmListTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
self.tableView.rowHeight = 200
self.tableView.separatorStyle = .None
}
}
//MARK: - tableView 协议方法
extension FilmListViewController:UITableViewDelegate,UITableViewDataSource{
//分组数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.dataArray.count
}
//每组cell的个数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//1.创建cell
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! FilmListTableViewCell
//2.刷新数据
let model = self.dataArray[indexPath.section] as! FilmListModel
//先将cell中的数据源数组清空
cell.dataArray.removeAllObjects()
//根据id去缓存中获取数据,如果获取到数据就直接将之前缓存的数据添加到数据源数组中
if let modelArray = self.filmDataCache.objectForKey(model.pagelist_id) {
cell.dataArray.addObjectsFromArray(modelArray as! [AnyObject])
}
cell.model = self.dataArray[indexPath.section] as? FilmListModel
//获取到从cell传过来的数据
cell.sendData = {(id,modelArrary) in
//保存到缓存中
self.filmDataCache.setObject(modelArrary, forKey: id)
}
//3.返回cell
return cell
}
}
影评
class HotCommentModel: NSObject,YYModel {
var created_at = ""
var film_id = ""
var film_name = ""
var large_pics = [String]()
var score = ""
var text = ""
var user:UserModel? = nil //YYModel可以直接将模型中属性转换成其他类型的模型
}
class UserModel: NSObject,YYModel {
var avatar_large = ""
var id = ""
var name = ""
}
class HotCommentFrameModel: NSObject {
//=======顶部======
//1.用户头像
var iconFrame = CGRectZero
//2.用户名
var userNameFrame = CGRectZero
//3.星级
var starFrame = CGRectZero
//4.时间
var timeFrame = CGRectZero
//=======中间======
//5.文字内容
var contentFrame = CGRectZero
//6.图片内容
var imageContenFrame = CGRectZero
//=======底部======
//7.底部背景
var bottomFrame = CGRectZero
//8.分割线
var lineFrame = CGRectZero
//9.电影名
var filmNameFrame = CGRectZero
//10.查看详情
var checkFrame = CGRectZero
//======cell高度======
//11.cell高度
var cellHeight:CGFloat = 0
//MARK: - 创建frameModel的时候,根据数据模型计算frame
init(dataModel:HotCommentModel) {
//通用:
let margin:CGFloat = 15
//=======顶部======
//1.用户头像
let iconX = margin
let iconY = margin
let iconW:CGFloat = 60
let iconH:CGFloat = 60
self.iconFrame = CGRectMake(iconX, iconY, iconW, iconH)
//2.用户名
let userX = iconX + iconW + margin
let userY = iconY
let userW = Screen_W - userX - margin
let userH:CGFloat = 21
self.userNameFrame = CGRectMake(userX, userY, userW, userH)
//3.星级
let starX = userX
let starH:CGFloat = 20
let starY = iconY + iconH - starH
let starW:CGFloat = 120
self.starFrame = CGRectMake(starX, starY, starW, starH)
//4.时间
let timeW:CGFloat = 100
let timeX = Screen_W - timeW - margin
let timeH:CGFloat = 21
let timtY = iconY + iconH - timeH
self.timeFrame = CGRectMake(timeX, timtY, timeW, timeH)
//=======中间======
//5.文字内容
let contentSize = ToolManager.calculateStringSize(dataModel.text, maxW: Screen_W - margin*2, maxH: 10000, fontSize: 13)
let contentW = contentSize.width
let contentH = contentSize.height
let contentX = margin
let contentY = iconY + iconH + margin
self.contentFrame = CGRectMake(contentX, contentY, contentW, contentH)
//6.图片内容
let imageX = margin
let imageY = contentY + contentH + margin
let imageW = Screen_W - margin*2
var imageH:CGFloat = 0
//不同的图片个数对应的基本高度不同:
let H1:CGFloat = 200 //一张图的基本高度
let H2:CGFloat = 150 //两张图的基本高度
let H3:CGFloat = 100 //三张图的基本高度
//图片的张数
let count = dataModel.large_pics.count
//没有图
if count == 0 {
imageH = 0
}
//一张图
if count == 1 {
imageH = H1
}
//两/四张图
if count==2 || count==4{
imageH = CGFloat(count/2) * H2
}
//其他情况
if count==3 || count>4 {
if count%3 == 0 {
imageH = CGFloat(count/3) * H3
}else{
imageH = CGFloat(count/3 + 1) * H3
}
}
self.imageContenFrame = CGRectMake(imageX, imageY, imageW, imageH)
//=======底部======
//7.底部背景
let bottomX:CGFloat = 0
let bottomY = imageY + imageH + margin
let bottomW = Screen_W
let bottomH:CGFloat = 44
self.bottomFrame = CGRectMake(bottomX, bottomY, bottomW, bottomH)
//8.分割线
let lineX:CGFloat = 0
let lineY:CGFloat = 0
let lineW = Screen_W
let lineH:CGFloat = 1
self.lineFrame = CGRectMake(lineX, lineY, lineW, lineH)
//9.电影名
let filmX = margin
let filmH:CGFloat = 21
let filmY = bottomH/2 - filmH/2
let filmW:CGFloat = 200
self.filmNameFrame = CGRectMake(filmX, filmY, filmW, filmH)
//10.查看详情
let checkW:CGFloat = 200
let checkX = Screen_W - margin - checkW
let checkY = filmY
let checkH = filmH
self.checkFrame = CGRectMake(checkX, checkY, checkW, checkH)
//11.cell的高度
self.cellHeight = bottomY + bottomH
}//init结束
}
import UIKit
import Kingfisher
//=======顶部======
//1.用户头像
//2.用户名
//3.星级
//4.时间
//=======中间======
//5.文字内容
//6.图片内容
//=======底部======
//7.底部背景
//8.分割线
//9.电影名
//10.查看详情
class HotCommentTableViewCell: UITableViewCell {
//MARK: - 第一步:声明cell上所有的子视图的属性
//=======顶部======
//1.用户头像
let iconImageView = UIImageView()
//2.用户名
let userNameLabel = UILabel()
//3.星级
let starView = StarView()
//4.时间
let timeLabel = UILabel()
//=======中间======
//5.文字内容
let conetentLabel = UILabel()
//6.图片内容
let imageContentView = ImageContentView()
//=======底部======
//7.底部背景
let bottomBgView = UIView()
//8.分割线
let lineView = UIView()
//9.电影名
let filmNameLabel = UILabel()
//10.查看详情
let checkLabel = UILabel()
//MARK: - 第二步:在构造方法中添加子视图,并且设置一次性属性
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//=======顶部======
//1.用户头像
self.contentView.addSubview(iconImageView)
//2.用户名
self.contentView.addSubview(userNameLabel)
userNameLabel.textColor = UIColor.lightGrayColor()
//3.星级
self.contentView.addSubview(starView)
//4.时间
self.contentView.addSubview(timeLabel)
timeLabel.textColor = UIColor.lightGrayColor()
timeLabel.font = UIFont.systemFontOfSize(12)
timeLabel.textAlignment = .Right
//=======中间======
//5.文字内容
self.contentView.addSubview(conetentLabel)
conetentLabel.font = UIFont.systemFontOfSize(13)
conetentLabel.numberOfLines = 0
//6.图片内容
self.contentView.addSubview(imageContentView)
//=======底部======
//7.底部背景
self.contentView.addSubview(bottomBgView)
//8.分割线
self.bottomBgView.addSubview(lineView)
lineView.backgroundColor = UIColor.lightGrayColor()
//9.电影名
self.bottomBgView.addSubview(filmNameLabel)
filmNameLabel.textColor = UIColor.lightGrayColor()
filmNameLabel.font = UIFont.systemFontOfSize(14)
//10.查看详情
self.bottomBgView.addSubview(checkLabel)
checkLabel.text = "查看电影详情"
checkLabel.textColor = UIColor.redColor()
checkLabel.font = UIFont.systemFontOfSize(13)
checkLabel.textAlignment = .Right
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: - 第三步,计算子视图的frame
var frameModel:HotCommentFrameModel? = nil{
didSet{
//=======顶部======
//1.用户头像
self.iconImageView.frame = frameModel!.iconFrame
//2.用户名
self.userNameLabel.frame = frameModel!.userNameFrame
//3.星级
self.starView.frame = frameModel!.starFrame
//4.时间
self.timeLabel.frame = frameModel!.timeFrame
//=======中间======
//5.文字内容
self.conetentLabel.frame = frameModel!.contentFrame
//6.图片内容
self.imageContentView.frame = frameModel!.imageContenFrame
//=======底部======
//7.底部背景
self.bottomBgView.frame = frameModel!.bottomFrame
//8.分割线
self.lineView.frame = frameModel!.lineFrame
//9.电影名
self.filmNameLabel.frame = frameModel!.filmNameFrame
//10.查看详情
self.checkLabel.frame = frameModel!.checkFrame
}
}
//MARK: - 第四步,显示数据
var dataModel:HotCommentModel? = nil{
didSet{
//=======顶部======
//1.用户头像
self.iconImageView.kf_setImageWithURL(NSURL.init(string: (dataModel!.user?.avatar_large)!)!, placeholderImage: UIImage.init(named: "Image_head"))
//2.用户名
self.userNameLabel.text = dataModel!.user?.name
//3.星级
let ocScore = NSString.init(string: (dataModel?.score)!)
self.starView.score = ocScore.floatValue
//4.时间
self.timeLabel.text = dataModel?.created_at
//=======中间======
//5.文字内容
self.conetentLabel.text = dataModel?.text
//6.图片内容
self.imageContentView.imageurlArray = dataModel?.large_pics
//=======底部======
//7.底部背景
//8.分割线
//9.电影名
self.filmNameLabel.attributedText = ToolManager.mixImageAndText("common_feed_film_name_dark", text: (dataModel?.film_name)!)
//10.查看详情
}
}
}
import Kingfisher
class ImageContentView: UIView {
//MARK: - 属性
//1.图片数组
var imageurlArray:[String]? = nil{
didSet{
//移除图片容器视图上原来的imageView
for item in self.subviews {
item.removeFromSuperview()
}
//根据图片数组创建对应的imageView
for item in imageurlArray! {
let imageView = UIImageView.init()
imageView.kf_setImageWithURL(NSURL.init(string: item)!, placeholderImage: UIImage.init(named: "movie_default_light_760x570"))
self.addSubview(imageView)
}
}
}
}
//MARK: - 计算frame
extension ImageContentView{
override func layoutSubviews() {
//获取图片张数
let count = self.imageurlArray?.count
//间距
let margin:CGFloat = 2
//容器的宽和高
let contentW = self.frame.size.width
let contentH = self.frame.size.height
//1.一张图
if count == 1 {
let x = margin
let y = margin
let w = contentW - margin*2
let h = contentH - margin*2
for item in self.subviews {
item.frame = CGRectMake(x, y, w, h)
}
return
}
//2.两张/四张图
if count==2 || count==4 {
let w = (contentW - margin*3)/2
let h = (contentH - margin * CGFloat(count!/2 + 1)) / CGFloat(count!/2)
for (i,item) in self.subviews.enumerate() {
let x = margin + (margin+w) * CGFloat(i%2)
let y = margin + (margin+h) * CGFloat(i/2)
item.frame = CGRectMake(x , y, w, h)
}
return
}
//3.三张/四张以上
if count==3 || count>4 {
let w = (contentW - 4*margin)/3
//行数
var line = 0
if count!%3 == 0 {
line = count!/3
}else{
line = count!/3 + 1
}
let h = (contentH - CGFloat(line+1)*margin) / CGFloat(line)
for (i,item) in self.subviews.enumerate() {
let x = margin + (margin + w) * CGFloat(i%3)
let y = margin + (margin + h) * CGFloat(i/3)
item.frame = CGRectMake(x, y, w, h)
}
}
}//
}
class FilmCommentViewController: BasicViewController {
//MARK: - 属性
//1.分段选择器
var segment:YTSegmentControl!
//2.滚动视图
let scrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBarItemSetting()
self.creatUI()
}
}
//MARK: - 界面相关
extension FilmCommentViewController{
override func creatUI() {
super.creatUI()
//1.滚动视图
self.scrollView.frame = self.view.bounds
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.width*2, self.scrollView.frame.height-64-49)
self.scrollView.pagingEnabled = true
self.scrollView.delegate = self
self.scrollView.showsHorizontalScrollIndicator = false
self.view.addSubview(self.scrollView)
//2.热门
let hotC = HotCommentController()
self.addChildViewController(hotC)
hotC.tableView.frame = self.scrollView.bounds
self.scrollView.addSubview(hotC.tableView)
//3.最新
let latestC = LatestCommentController()
self.addChildViewController(latestC)
latestC.tableView.frame = CGRectMake(self.scrollView.frame.width, 0, self.scrollView.frame.width, self.scrollView.frame.height)
self.scrollView.addSubview(latestC.tableView)
}
override func navigationBarItemSetting() {
super.navigationBarItemSetting()
//1.中间视图
//创建对象
self.segment = YTSegmentControl.init(items: ["热门","最新"])
segment.frame = CGRectMake(0, 0, 100, 44)
self.navigationItem.titleView = segment
//设置属性
segment.titleColor = UIColor.whiteColor()
segment.titleSelectedColor = UIColor.whiteColor()
segment.selectedFont = UIFont.boldSystemFontOfSize(15)
segment.normalFont = UIFont.systemFontOfSize(12)
segment.sliderColor = UIColor.yellowColor()
//添加事件
segment.addTarget(self, action: "segmentAction:")
}
}
//MARKL - scrollView Delegate
extension FilmCommentViewController:UIScrollViewDelegate{
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if scrollView.contentOffset.x == 0{
self.segment.selectedSegmentIndex = 0
return
}
self.segment.selectedSegmentIndex = 1
}
}
//MARK: - 按钮点击
extension FilmCommentViewController{
func segmentAction(segment:YTSegmentControl) {
if segment.selectedSegmentIndex == 0 {
print("热门")
self.scrollView.contentOffset = CGPointMake(0, 0)
}else{
print("最新")
self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.width, 0)
}
}
}
import Alamofire
//MARK: - 属性
class HotCommentController: UITableViewController {
//1.数据源数组
lazy var dataArray:NSMutableArray = {
return NSMutableArray()
}()
//2.frame数组
lazy var frameArray:NSMutableArray = {
return NSMutableArray()
}()
}
//MARK: - tableView 协议方法
extension HotCommentController{
//设置分组数
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.dataArray.count
}
//每个分组cell的个数
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
//创建cell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//1.创建cell
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! HotCommentTableViewCell
//2.刷新数据
cell.dataModel = self.dataArray[indexPath.section] as? HotCommentModel
cell.frameModel = self.frameArray[indexPath.section] as? HotCommentFrameModel
//3.返回cell
return cell
}
//设置cell的高度
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let frameModel = self.frameArray[indexPath.section] as! HotCommentFrameModel
return frameModel.cellHeight
}
}
//MARK: - 生命周期
extension HotCommentController{
override func viewDidLoad() {
super.viewDidLoad()
//获取数据
self.getNetData(0)
//注册cell
self.tableView.registerClass(HotCommentTableViewCell.self, forCellReuseIdentifier: "cell")
}
}
//MARK: - 数据请求
extension HotCommentController{
func getNetData(page:Int){
Alamofire.request(.GET, Url_FilmComment, parameters: ["count":10,"type":1], encoding: .URL, headers: nil).responseJSON(options: .MutableContainers) { (data) in
//解析数据
if let json = data.result.value{
let dataDict = json.objectForKey("data") as! NSDictionary
//拿到字典数组
let feedListArray = dataDict["feed_list"]
//将字典数组转换成模型数组
let modelArray = NSArray.yy_modelArrayWithClass(HotCommentModel.self, json: feedListArray)
//变量模型数组,通过数据模型创建对应的frame模型
for item in modelArray{
let model = item as! HotCommentModel
let frameModel = HotCommentFrameModel.init(dataModel: model)
self.frameArray.addObject(frameModel)
}
//将模型添加到数据源数组中
self.dataArray.addObjectsFromArray(modelArray)
//刷新tableView
self.tableView.reloadData()
}