Xcode 7.3环境下Swift 创建TableView

一、UITableView创建(分段代码)

1、怎么创建swift的项目这里就不详细写了,直接上代码创建tableview
(1.)遵守tableview的Delegate及DataSource
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource

// 定义一个数组
 var array = ["兔子0","兔子1","兔子2","兔子3","兔子4","兔子5","兔子6","兔子7","兔子8","兔子9","兔子10","兔子11"]
(2.) 创建tableview,并添加到view上
// 定义一个tableView
var tableView = UITableView()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tableView = UITableView(frame: CGRectMake(0, 64, view.bounds.width, view.bounds.height - 64) ,style: UITableViewStyle.Plain)
        // 在这一步之前 ,先遵守tableview的Delegate及DataSource
        tableView.delegate = self
        tableView.dataSource = self
        // 将tableView添加到View上
        self.view.addSubview(tableView)
}
(3.) 实现代理方法与数据源方法
    // cell 的高度
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60
    }
    // 返回组数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    // 返回行数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    // cell的DataSource
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let initIdentifier = "Cell"
        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: initIdentifier)
        // 设置cell的文字
        cell.textLabel?.text = array[indexPath.row]
        // 设置描述文字
        cell.detailTextLabel?.text = "baby\(indexPath.row + 1)"
        // 设置cell 图片
        cell.imageView?.image = UIImage(named: "1024")
        return cell
    }

二、全部代码

import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
    var array = ["兔子0","兔子1","兔子2","兔子3","兔子4","兔子5","兔子6","兔子7","兔子8","兔子9","兔子10","兔子11"]

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let initIdentifier = "Cell"
        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: initIdentifier)
        
        cell.textLabel?.text = array[indexPath.row]
        cell.detailTextLabel?.text = "baby\(indexPath.row + 1)"
        cell.imageView?.image = UIImage(named: "1024")
        return cell
    }
    
    
    var lableTitle = UILabel()
    
    
    var tableView = UITableView()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        tableView = UITableView(frame: CGRectMake(0, 64, view.bounds.width, view.bounds.height - 64) ,style: UITableViewStyle.Plain)
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
        
        lableTitle = UILabel()
        lableTitle.text = "TableView"
        lableTitle.textColor = UIColor .redColor()
        lableTitle.textAlignment = NSTextAlignment.Center
        lableTitle.frame = CGRectMake(0, 0, view.bounds.width, 64)
        lableTitle.backgroundColor = UIColor .grayColor()
        self.view.addSubview(lableTitle)
    }
}

三、上一个预览图

Paste_Image.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值