Swfit 学习tableview
classOnrViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{
//创建tableview
var tableView :UITableView?
letURL :String="http://piao.163.com/m/movie/list.html?type=0&city=110000&apiVer=14&mobileType=android&deviceId=r4a5ba63afbabd7a70ceeaf8485f7942e&channel=wandoujia&ver=4.9"
//创建一个数组
vardataSource:NSArray=NSArray()
overridefuncviewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
overridefuncviewDidAppear(animated:Bool) {
super.viewDidAppear(animated)
//创建UI
creatUI()
//创建数据源
createDataSource()
}
funccreatUI() {
tableView=UITableView(frame:self.view.bounds,style: .Plain)
tableView!.delegate=self
tableView!.dataSource=self
tableView!.separatorStyle= .SingleLineEtched
self.view.addSubview(tableView!)
}
funccreateDataSource (){
leturl:NSURL=NSURL(string:URL)!
letrequest :NSURLRequest=NSURLRequest(URL:url)
NSURLConnection.sendAsynchronousRequest(request, queue:NSOperationQueue.mainQueue()) { (response, data, error)in
letjson :AnyObject=try!NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
self.dataSource= json.objectForKey("list")as!NSArray
self.tableView!.reloadData()
}
}
//代理方法
functableView(tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
returnself.dataSource.count;
}
//代理方法获取cell
functableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{
letidentifer ="tableviewcell"
varcell = tableView.dequeueReusableCellWithIdentifier(identifer)
if(cell ==nil) {
cell =UITableViewCell(style:.Default,reuseIdentifier: identifer)
cell!.selectionStyle= .None
}
// cell!.text Label!.text = "tableviewcell"
letobj:NSDictionary=self.dataSource[indexPath.row]as!NSDictionary
letname:String= obj.objectForKey("name")as!String
cell!.textLabel!.text= name
cell!.backgroundColor=UIColor.blueColor()
returncell!
}
functableView(tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
if(indexPath.row==0) {
print("sdadadadad")
}
}
functableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath) ->CGFloat{
return200
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。