开始用Swift开发iOS 10 - 6 创建简单的Table Based App


table view应该是iOS应用中最常用的UI element。最好的例子就是iPhone自带的一些应用,如电话,邮件,设置等。TED,Google+,Airbnb,微信等等都是很好例子。

创建一个项目

  • 项目名称为SimpleTable,模板为"Single View application"

设计UI

  • 选中Main.storyboard,从Object library中拖动Table View进入视图
  • 改变Table View的大小至整个view,修改属性Prototype Cells为1
  • 选中Table View Cell,修改StyleBasicIdentifierCell。table view cell的标准类型有 basic、right detail、left detail 和 subtitle,当然还有定制类型custom。
  • 选中Table View,设置四个spacing约束,上下左右的距离都设置为0

为UITableView添加两个协议

  • Object library中的每一UI component都是对应一个class,如 Table View就是对应UITableView。可以通过点击并悬停在UI component上查看对应的class和介绍。
  • ViewController.swift文件的UIViewController后,添加代码, UITableViewDataSource, UITableViewDelegate,表示ViewController类实现了UITableViewDataSourceUITableViewDelegate两个协议。
    出现红色感叹号,这是xcode的问题提示,点击参看问题描述:

Type 'ViewController' does not conform to protocol
'UITableViewDataSource'

问题描述为ViewController不符合协议UITableViewDataSource。通过command+点击 (最新的xcode9变成了command+option+点击)到UITableViewDataSource中看看你:

public protocol UITableViewDataSource : NSObjectProtocol {

    
    @available(iOS 2.0, *)
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    
    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
    
    @available(iOS 2.0, *)
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    
    @available(iOS 2.0, *)
    optional public func numberOfSections(in tableView: UITableView) -> Int // Default is 1 if not implemented

    
    @available(iOS 2.0, *)
    optional public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different

    @available(iOS 2.0, *)
    optional public func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?

    
    // Editing
    
    // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
    @available(iOS 2.0, *)
    optional public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool

 ...
  • UITableViewDataSource协议中定义了很多方法,除了前两个方法没有optional其它都有,有的表示这个方法不一定要实现,没有的就一定要实现,把这个两个方法实现了,问题提示就会消失。这两个方法从名字和返回值类型也大概能知道做了什么:

    • public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 一个section有几行,也就是一个section有几个UITableViewCell, section就是一组UITableViewCell的意思,Table View可以定义多个section,默认是一个。
    • public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 返回每一行的 UITableViewCell
  • ViewController.swift中定义一个变量restaurantNames,类型是数组,表示一系列餐馆的名字。

var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "PetiteOyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh'sChocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats AndDeli", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional","Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
  • 定义UITableViewDataSource的两个方法:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // 1
        return restaurantNames.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
        UITableViewCell {
        // 2
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier,
                                                     for: indexPath)
        // 3
        cell.textLabel?.text = restaurantNames[indexPath.row]
        return cell
            
    }
  • 1 餐馆的数目就是section的行数
  • 2 "Cell"与之前定义的UITableViewCellIdentifier属性是对应的。dequeueReusableCell方法是产生一个UITableViewCell
  • 3 UITableViewCell中有可算属性textLabel,其实就是一个UILabel,由于是可选属性,调用时也用可选链式调用cell.textLabel?.text

连接 DataSource 和 Delegate

运行app,没有数据显示。尽管上面已经在代码中让ViewController继承了UITableViewDataSource, UITableViewDelegate,但storyboard并不知道。

  • 在document outline中选择Table View,使用control-dragView Controller,在弹出框中选择dataSource。同样的方法选择delegate
  • 确认连接是否成功。选中Table View,在connetion检查器中查看;或者直接在document outline中右击Table View
  • 运行app


添加图片到Table View

  • simpletable-image1.zip下载图片,解压后一起拖到asset catalog
  • ViewController.swifttableView(_:cellForRowAtIndexPath:)方法的return cell前添加代码cell.imageView?.image = UIImage(named: "restaurant")。使每个UITableViewCell的image都是一样的。

隐藏状态栏

顶部状态来和table view 数据重叠了,只要在ViewController加一段代码就可以:

override var prefersStatusBarHidden: Bool {
    return true
}

prefersStatusBarHidden是父类UIViewController中的属性,所以要加 override,表示重写了。

不同的Cell对应不同的图片

  • simpletable-image2.zip下载图片,解压后一起拖到asset catalog
  • 修改ViewController.swifttableView(_:cellForRowAtIndexPath:)为:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
        UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier,
                                                     for: indexPath)
        // Configure the cell...
        let restaurantName = restaurantNames[indexPath.row]
        cell.textLabel?.text = restaurantName
        // 1
        let imageName = restaurantName.lowercased().replacingOccurrences(of: " ", with: "")
        if let image = UIImage(named: imageName) {
            cell.imageView?.image = image
        } else {
            cell.imageView?.image = UIImage(named: "restaurant")
        }
        
        return cell
            
    }
  • 1 把菜馆名字字符串先修改成小写,然后去空格
  • 最终结果

代码

Beginning-iOS-Programming-with-Swift

说明

此文是学习appcode网站出的一本书 《Beginning iOS 10 Programming with Swift》 的一篇记录

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容