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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,192评论 4 61
  • 有一个男生追求女生,女生也有好感。两人上街,女生说:“我的包好重哟。”男生接过来,“嗯,真的好重。”男生把包还给了...
    月梦晚风阅读 1,844评论 8 8
  • 文 / 西门君 图 / 网络 1. 昨天刷到这么一条微博,阅完之后,我觉得有必要说些什么。 所有的投票里,我最鄙视...
    西门君不吐槽阅读 342评论 0 0
  • 那是我认识的一个女孩,名字叫默默,学神一枚,上课睡觉,回宿舍睡觉,晚上追剧,考试前一天随便一读,成绩始终前三。这是...
    柒拾壹阅读 232评论 0 0
  • 一、通用头像,性别区分 问题:同质化严重,不易区别,需要填写性别 二、有趣的艺术图案作为头像 问题:识别度不高,利...
    莫同学vv阅读 2,254评论 0 1