swift 开眼今日精选 import UIKit
class TodayController: UITableViewController {
vararray =NSMutableArray() overridefuncviewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(Today Cell.self,forCellReuseIdentifier:"todayCel l")
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
//定义每个cell的高度
self.tableView.rowHeight = kScreenWith / 1242 * 777
letformatter =NSDateFormatter() formatter.dateFormat = "yyyyMMhh" formatter.timeZone =
NSTimeZone.systemTimeZone() letdate =
formatter.stringFromDate(NSDate())
leturl =NSURL(string: "http://baobab.wandoujia.com/api/v1/feed?d ate=(date)&num=10")
let request = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRe quest(request, queue:
NSOperationQueue.mainQueue()) { (response, data , error) ->Voidin
if((error) !=nil){
}else{ //try是Xcode7需要写的,6不用写 letdic =
NSJSONSerialization.JSONObjectWithData(dat a!, options: NSJSONReadingOptions.AllowFragments, error: nil) as!NSDictionary
for dailyList in dic["dailyList"] as! NSArray{
letvideoListArray = dailyList.objectForKey("videoList")as!NSAr
ray
// let
videoListArray = dailyList.objectForKey("videoList") as!NSArray
NSMutableArray()
videoListArray{ TodayModel()
//创建一个subArray letsubArray =
for videoListDic in letmodel =
model.setValuesFor KeysWithDictionary(videoListDic as!
[NSObject : AnyObject]) (model)
ubArray)
)
subArray.addObject }
self.array.addObject(s }
self.tableView.reloadData( }
} }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning() }
override func
numberOfSectionsInTableView(tableView: UITableView) ->Int{
return array.count }
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int{
return
array.objectAtIndex(section).count }
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{
letcell = tableView.dequeueReusableCellWithIdentifie r("todayCell", forIndexPath: indexPath) as! TodayCell
cell.model = array.objectAtIndex(indexPath.section).obj ectAtIndex(indexPath.row) as!TodayModel
return cell }
}
import UIKit
class TodayModel: NSObject {
var category = ""
var coverBlurred = ""
var coverForDetail = ""
var date = 0.0
var myDescription = ""
var duration = 0
var playUrl = ""
var title = ""
var webUrl = ""
override func setValue(value: AnyObject?, forUndefinedKey key: String) {
if key == "description"{ setValue(value, forKey:
"myDescription") }
} }
import UIKit
class TodayCell: UITableViewCell {
var icon:UIImageView! var label:UILabel! var styleL:UILabel! //定义一个Model
var model:TodayModel!{ didSet{
label.text = model.title
styleL.text = model.category icon.sd_setImageWithURL(NSURL(stri
ng: model.coverForDetail)) }
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier:reuseIdentifier)
let height = kScreenWith / 1242 * 777
icon = UIImageView(frame: CGRectMake(0, 0, kScreenWith, height))
self.contentView.addSubview(icon)
let y = (height - 80) / 2
label = UILabel(frame: CGRectMake(0, y, kScreenWith, 40))
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
self.contentView.addSubview(label)
styleL = UILabel(frame: CGRectMake(0, y + 40, kScreenWith, 40))
styleL.textColor = UIColor.whiteColor()
styleL.textAlignment = NSTextAlignment.Center
self.contentView.addSubview(styleL
)
}
required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not
been implemented") }
}