Swift学习之--TableView的基本使用

每个iOS开发人员都知道tableview,因为它是我们开发中最常用的Contrller了.下面就简单介绍Swift中tableview的一些简单的使用:
(先看效果图)
这里写图片描述
下面直接贴代码

class ViewController: UIViewController{

    override func loadView() {
        let tv = UITableView()
        tv.frame = UIScreen.mainScreen().bounds
        tv.dataSource = self
        tv.delegate = self
        tv.separatorColor = UIColor.lightGrayColor()
        view = tv
    }

    override func viewDidLoad() {
        //修改导航栏背景色
        navigationController?.navigationBar.barTintColor =
            UIColor(red: 55/255, green: 186/255, blue: 89/255, alpha: 1)
        //修改导航栏文字颜色
        navigationController?.navigationBar.titleTextAttributes =
            [NSForegroundColorAttributeName: UIColor.whiteColor()]
        //修改导航栏按钮颜色
        navigationController?.navigationBar.tintColor = UIColor.whiteColor()
        //添加标题
        navigationItem.title = "TableView基本使用"
        //修改导航栏背景图片
//        navigationController?.navigationBar.setBackgroundImage(UIImage(named: "bg5"), forBarMetrics: .Default)
    }

    // MARK:-
    // MARK:-懒加载数据
    lazy var dataList:[String] = {
        return ["rookic","yx","toxic","mine"]
    }()
}
// MARK:-
// MARK:- 苹果官方建议, 可以将数据源代理方法单独写到一个扩展中,以便提高代码的可读性
// extension 相当于OC中的 catogory
extension ViewController:UITableViewDelegate,UITableViewDataSource
{
    // MARK:-
    // MARK:-UITableViewDataSource
    //  共多少行
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    // 每行显示什么内容
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let str = "cellID"
        // 1 取出cell
        var cell = tableView.dequeueReusableCellWithIdentifier(str)
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: str)
        } 
        // 2 设置数据
        cell?.textLabel?.text = dataList[indexPath.row]
        cell?.textLabel?.textColor = UIColor.redColor()
        // 3 返回cell
        return cell!
    }
}

新博客地址

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值