嘗試下swift些UI的快感,然而第一接觸還是有點(diǎn)不順手光稼,一個(gè)TableView也研究了一下或南,方便了許多。都說swift將成為iOS開發(fā)的必然艾君,趕緊學(xué)習(xí)下采够,要不落伍了。上代碼吧
1.pic.jpg
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
var tableView:UITableView?
var dataSource = ["北京","上海","深圳","天津","河北","湖北","江蘇","浙江","武漢"];
override func viewDidLoad() {
super.viewDidLoad()
self.makeTableView()
}
func makeTableView(){
self.tableView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.Plain)
self.tableView!.dataSource = self;
self.tableView!.delegate = self;
self.view.addSubview(self.tableView!)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "cell"
let cell = UITableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: identifier)
cell.textLabel?.text = dataSource[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated:true)
let str = dataSource[indexPath.row]
print(str)
}
}