//
// ViewController.swift
// myview
//
// Created by 黄权浩 on 14-12-27.
// Copyright (c) 2014年 黄权浩. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
//tableView的协议(在swift中你必须实现协议中的必实现协议,否则编译器会报错)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//声明一个变量它的类型是表视图 并且初始化了它的fram
var mytableView = UITableView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
//实现tableView的协议
mytableView.delegate = self
mytableView.dataSource = self
self.view .addSubview(mytableView)//加入当前视图控制器
}
//表视图协议方法
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 20;
}//表视图的返回行数
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let CellId="my cell";//cell标识符
var cell = tableView.dequeueReusableCellWithIdentifier(CellId) as? UITableViewCell;//创建cell
if(cell==nil)
{
cell=UITableViewCell(style:.Default,reuseIdentifier:CellId);
}
//其实跟oc完全是大同小异
cell?.textLabel?.text = "\(indexPath.row)"//附值
return cell!;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
swift中表视图的创建
最新推荐文章于 2022-04-15 15:22:55 发布