Swift4.0 study2 初始化项目 tableView

经过第一天的学习,对Swift语法已基本会用,今天就就来初始化一下项目,在项目中进行实战学习。(发布的这些博客 完全按照自己的学习思路进行发布)

新建项目及模块文件夹的过程和oc一致,只要记得语言选择Swift。在AppDelegate中初始化跟控制器,

window = UIWindow(frame:UIScreen.main.bounds)
window?.backgroundColor = UIColor.white
window?.rootViewController = UIViewController()
window?.makeKeyAndVisible()


接下来 我要做列表视图 tableView
定义全局变量

var tableView = UITableView()
var dataArray:NSMutableArray = []

初始化tableView并遵循代理,遵循代理直接在父类的后面用逗号隔开

tableView = UITableView.init(frame: self.view.frame, style:UITableViewStyle.plain)
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)

实现代理方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return dataArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView .dequeueReusableCell(withIdentifier: "CellID")
    if cell == nil
    {
        cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: "CellID")
    }
    cell?.textLabel?.text = (dataArray[indexPath.row] as! String)
      return cell! 
 }

加载数据刷新列表

dataArray = ["s","c","h"]
tableView.reloadData()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值