新手练习Swift版tableview
// ViewController.swift
// Swift_lianxi
// Created by dllo on 16/1/7.
// Copyright © 2016年 z_han. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableView :UITableView?
var dataArray :NSMutableArray?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.dataArray = NSMutableArray()
tableView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.Plain)
tableView?.delegate = self
tableView?.dataSource = self
self.view.addSubview(tableView!)
tableView?.registerClass(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
tableView?.rowHeight = 110
self.getData()
}
func getData(){
let manager :AFHTTPSessionManager = AFHTTPSessionManager()
// 支持的接口类型
manager.responseSerializer.acceptableContentTypes = NSSet(objects:"text/plain","text/json","application/json","text/javascript","text/html") as? Set<String>
manager.GET("http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php", parameters: nil, progress: nil, success: { (tast: NSURLSessionDataTask, responseObject:AnyObject?) -> Void in
// print(responseObject)
let responseObject :NSDictionary = responseObject as! NSDictionary
let resultArray :NSArray = responseObject.objectForKey("result") as!NSArray
// print(resultArray)
for dic in resultArray{
let resultDic :NSDictionary = dic as! NSDictionary
let model :Model = Model()
// 赋值
model.setValuesForKeysWithDictionary(resultDic as![String : AnyObject])
self.dataArray?.addObject(model)
}
self.tableView?.reloadData()
}) { (task:NSURLSessionDataTask?, error :NSError) -> Void in
print("失败 (error)")
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (self.dataArray?.count)!;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell :TableViewCell? = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as? TableViewCell
let model = self.dataArray![indexPath.row]
cell?.model = model as? Model
cell?.selectionStyle = UITableViewCellSelectionStyle.None
return cell!
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let detailVC :DetailViewController? = DetailViewController()
self.presentViewController(detailVC!, animated: true, completion: nil)
}
// TableViewCell.swift
// Swift_lianx
//
// Created by dllo on 16/1/7.
// Copyright © 2016年 z_han. All rights reserved.
//
import UIKit
class TableViewCell: UITableViewCell {
var imageVieg :UIImageView?
var titleLabel :UILabel?
var bgImage :UIImageView?
var model :Model?{
set(newModel){
let url :NSURL = NSURL.init(string: (newModel?.pic_url)!)!
self.imageVieg?.sd_setImageWithURL( url, placeholderImage: UIImage(named: "picholder")!)
self.titleLabel?.text = newModel?.movieName
}
get{
return self.model
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// 背景
bgImage = UIImageView.init(image: UIImage(named: "bg_eventlistcell"))
bgImage?.frame = CGRectMake(10, 10, 375 - 20, 100)
self.contentView.addSubview(bgImage!)
// 图片
imageVieg = UIImageView.init(image: UIImage(named: "picholder"))
imageVieg?.frame = CGRectMake(10, 10, 60, 80)
bgImage?.addSubview(imageVieg!)
// 标题
titleLabel = UILabel.init(frame: CGRectMake(90, 20, 375 - 10-10-60-20, 60))
bgImage?.addSubview(titleLabel!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}