swift中表视图的创建

来源:互联网 发布:如何自学matlab知乎 编辑:程序博客网 时间:2024/05/11 13:04
////  ViewController.swift//  myview////  Created by 黄权浩 on 14-12-27.//  Copyright (c) 2014年 黄权浩. All rights reserved.//import UIKitclass ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{//tableView的协议(在swift中你必须实现协议中的必实现协议,否则编译器会报错)    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        //声明一个变量它的类型是表视图 并且初始化了它的fram        var mytableView = UITableView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))        //实现tableView的协议        mytableView.delegate = self        mytableView.dataSource = self        self.view .addSubview(mytableView)//加入当前视图控制器    }    //表视图协议方法    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{        return 20;    }//表视图的返回行数        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{        let CellId="my cell";//cell标识符        var cell = tableView.dequeueReusableCellWithIdentifier(CellId) as? UITableViewCell;//创建cell        if(cell==nil)        {            cell=UITableViewCell(style:.Default,reuseIdentifier:CellId);        }        //其实跟oc完全是大同小异        cell?.textLabel?.text = "\(indexPath.row)"//附值        return cell!;    }        override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}


0 0
原创粉丝点击