iOS使用Swift创建UITableView

WWDC 2014上,苹果公布了全新的编程语言Swift,这两天利用业余时间大概研究了一下,感觉Swift挺不错的。用Swift创建的文件不再有.h和.m之分,而是统一采用swift作为后缀。swift代码跟OC不能写在一个文件中,需要分成两个文件来写。使用OC创建文件后,会自动生成一个Hello-Bridging-Header.h头文件,把OC写的.h文件import到这个Bridging文件中即可。

闲来无事,用Swift初始化了一个UITableView,拿出来跟各位看官分享一下Swift的语法。具体实现代码如下:

import UIKit

// 跟OC一样,这个位置需要引入代理
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        // 初始化tablview
        var tableView: UITableView = UITableView(frame: CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: self.view.frame.size.height), style: UITableViewStyle.Plain)
        // 设置背景色
        tableView.backgroundColor = UIColor.orangeColor()
        // 设置代理
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // #pragma mark - UITableViewDataSource
    func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: nil)
        cell.textLabel.text = "\(indexPath.row)"
        return cell
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值