swift语言IOS8开发战记2.tableview

简介:    上一章简单介绍了Swift写的button和alert,今天来学习一下tableview的用法。

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


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

import UIKit

class 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 = self
        tableView.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 UIKit

class 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 = self
        tableView.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一样。 顺便温习了上一话中的知识,运行效果图如下:


目录
相关文章
|
26天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
83 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
38 0
|
3月前
|
存储 安全 Swift
Swift 语言:什么是 Swift 的泛型(Generics)?
Swift 语言:什么是 Swift 的泛型(Generics)?
35 0
|
3月前
|
Swift iOS开发
Swift 语言: 什么是协议(Protocol)?如何实现和使用协议?
Swift 语言: 什么是协议(Protocol)?如何实现和使用协议?
41 2
|
3月前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
42 2
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
89 2
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
181 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4
|
3月前
|
iOS开发
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
26 1

相关课程

更多