swift语言IOS8开发战记2.tableview

上一章简单介绍了Swift写的button和alert,今天来学习一下tableview的用法。tableview看字面就知道,是列表组件。新建一个名为tableview的single view,步骤不罗嗦了。拖拽创建一个tableview,然后添加一个cell,prototype cells设为1,可以看到table中有一个cell。如图所示:

然后我们在代码中添加代码,想要在tableview中显示内容,需要一个数据源datasource,而数据源需要代理实现,只添加数据源还不够,需要添加数据源的方法,Xcode强大的代码不全给我们带来了很大的便利,比如我现在想要返回十行,只有一个组。代码如下:

import UIKitclass ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {override func viewDidLoad() {super.viewDidLoad()let tableView = UITableView(frame: CGRectMake(0, 0, 320, 568), style: UITableViewStyle.Plain)self.view.addSubview(tableView)tableView.dataSource = selftableView.delegate = self// Do any additional setup after loading the view, typically from a nib.}func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return 10}func numberOfSectionsInTableView(tableView: UITableView) -> Int {return 1;}}

如果要在上面的行中增加内容,也就是添加cell的具体内容,添加以下方法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {let identiString = "Cell"var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: identiString)cell.textLabel?.text = "cg Swift"return cell}这个方法让每一行都输出一个“cg Swift”但是我们现在运行程序的话会发现tableview中是空的,那是因为我们没有把代码和显示关联起来。切换到图形页面中,不过这次的关联和第一话中的关联方法并不一样,,不是把控件拖拽到代码中,而是右击tableview,把第一行中的datasource和delegate拖拽到tableview中,如图所示:

然后再运行,效果如下:

现在,我们来让tableview的内容变得复杂一点。声明一个数组,并且让tableview的每一行都显示数组的对应内容,点击后弹出提醒框,行数打印到控制台。代码如下:

import UIKitclass ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {var restaurantNames = ["cg1","cg2","cg3","cg4","cg5","cg6","cg7","cg8","cg9","cg10","cg11"]override func viewDidLoad() {super.viewDidLoad()let tableView = UITableView(frame: CGRectMake(0, 0, 320, 568), style: UITableViewStyle.Plain)self.view.addSubview(tableView)tableView.dataSource = selftableView.delegate = self// Do any additional setup after loading the view, typically from a nib.}func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return restaurantNames.count}func numberOfSectionsInTableView(tableView: UITableView) -> Int {return 1;}func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {let identiString = "Cell"var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: identiString)cell.textLabel?.text = restaurantNames[indexPath.row]return cell}override func prefersStatusBarHidden() -> Bool { //隐藏上边栏中的电量、信号等信息return true}func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {print(indexPath.row)var message = restaurantNames[indexPath.row]let alertView = UIAlertView(title: nil, message: message, delegate: nil, cancelButtonTitle: "Cancle")alertView.show()}}其中tableView.dataSource = self的意思是viewClntroller本身充当了tableview的数据源,注意此时我们手动添加viewController的一个集成UITableViewDelegate,这样可以不用去图形界面拖拽添加delegate,如果不添加的话,点击每一个cell是没反应的,tableView.datadelegate = self的设置,原理同datasource一样。 顺便温习了上一话中的知识,运行效果图如下:

累死累活不说,走马观花反而少了真实体验,

swift语言IOS8开发战记2.tableview

相关文章:

你感兴趣的文章:

标签云: