加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swfit 学习tableview

发布时间:2020-12-15 20:03:29 所属栏目:百科 来源:网络整理
导读:class ?OnrViewController: UIViewController , UITableViewDelegate ,174);">UITableViewDataSource { ? ?? // 创建 tableview ? ?? var ? tableView :? UITableView ? ? ?? let ?URL : String ?=? "http://piao.163.com/m/movie/list.html?type=0city=1100

class?OnrViewController:UIViewController,UITableViewDelegate,174);">UITableViewDataSource{

? ??//创建tableview

? ??var? tableView :?UITableView?

? ??let?URL :String?=?"http://piao.163.com/m/movie/list.html?type=0&city=110000&apiVer=14&mobileType=android&deviceId=r4a5ba63afbabd7a70ceeaf8485f7942e&channel=wandoujia&ver=4.9"

? ??创建一个数组

? ??var?dataSource:NSArray?=?NSArray()

?? ?

? ??override?func?viewDidLoad() {

? ? ? ??super.viewDidLoad()

? ? ? ??// Do any additional setup after loading the view.

?? ? ?

? ? }

?? ?

? ??func?viewDidAppear(animated:?Bool) {

? ? ? ??viewDidAppear(animated)

? ? ? ??UI

? ? ? ??creatUI()

? ? ? ???数据源

? ? ? ??createDataSource()

? ? }

?? ?

? ??func?creatUI() {

? ? ? ??tableView?=?UITableView(frame:?self.view.bounds,style: .Plain)

? ? ? ??tableView!.delegate?=?self

? ? ? ??dataSource?=?separatorStyle?= .SingleLineEtched

?? ? ? ?

? ? ? ??view.addSubview(tableView!)

? ? }

?? ?

? ??func?createDataSource (){

? ? ? ??let?url:NSURL?=?NSURL(string:?URL)!

? ? ? ??let?request :?NSURLRequest?=?NSURLRequest(URL:url)

? ? ? ??NSURLConnection.sendAsynchronousRequest(request,queue:NSOperationQueue.mainQueue()) { (response,data,error)?in

? ? ? ? ? ??let?json :AnyObject?=try!NSJSONSerialization.JSONObjectWithData(data!,options: .AllowFragments)

? ? ? ? ? ??self.dataSource?= json.objectForKey("list")?as!NSArray

? ? ? ? ? ??tableView!.reloadData()

? ? ? ? }

?? ? ? ?

? ? }

?? ?

? ??代理方法

? ??func?tableView(tableView:?UITableView,numberOfRowsInSection section:?Int) ->?Int?{

? ? ? ??return?dataSource.count;

? ? }

?? ?

? ??代理方法获取cell

? ??NSIndexPath) ->?UITableViewCell?{

? ? ? ??let?identifer =?"tableviewcell"

? ? ? ??var?cell = tableView.dequeueReusableCellWithIdentifier(identifer)

? ? ? ??if?(cell ==?nil) {

? ? ? ? ? ? cell =?UITableViewCell(style:.Default,reuseIdentifier: identifer)

? ? ? ? ? ? cell!.selectionStyle?= .None

? ? ? ? }

//? ? ? ? cell!.text Label!.text = "tableviewcell"

? ? ? ??let?obj:NSDictionary?=?dataSource[indexPath.row]?NSDictionary

? ? ? ??let?name:String?= obj.objectForKey("name")as!String

?? ? ? ?

?? ? ? cell!.textLabel!.text?= name

? ? ? ? cell!.backgroundColor?=?UIColor.blueColor()

?? ? ??return?cell!

? ? }

?? ?

? ??NSIndexPath) {

? ? ? ??if?(indexPath.row?==?0) {

? ? ? ? ? ??print("sdadadadad")

? ? ? ? }

? ? }

?? ?

? ??NSIndexPath) ->?CGFloat?{

? ? ? ??return?200

? ? }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读