開始用Swift開發(fā)iOS 10 - 13 Self Sizing Cells and Dynamic Type

接著上一遍開始用Swift開發(fā)iOS 10 - 12 豐富Detail View和定制化Navigation Bar的代碼稍作修改汪茧,為Restaurant多加一個phone屬性,修改過location的長度萎攒。

  • 修改RestaurantTableViewController中的restaurants變量(添加字段phone馏锡,location字段內容增多):
    var restaurants:[Restaurant] = [
        Restaurant(name: "Cafe Deadend", type: "Coffee & Tea Shop",location: "G/F, 72 Po Hing Fong, Sheung Wan, Hong Kong", phone: "232-923423",image: "cafedeadend.jpg", isVisited: false),
        Restaurant(name: "Homei", type: "Cafe", location: "Shop B, G/F, 22-24A Tai Ping San Street SOHO, Sheung Wan, Hong Kong", phone: "348-233423", image:"homei.jpg", isVisited: false),
        Restaurant(name: "Teakha", type: "Tea House", location: "Shop B, 18 Tai Ping Shan Road SOHO, Sheung Wan, Hong Kong", phone: "354-243523", image: "teakha.jpg", isVisited: false),
        Restaurant(name: "Cafe loisl", type: "Austrian / Causual Drink", location: "Shop B, 20 Tai Ping Shan Road SOHO, Sheung Wan, Hong Kong", phone: "453-333423", image: "cafeloisl.jpg", isVisited: false),
        Restaurant(name: "Petite Oyster", type: "French", location: "24 Tai Ping Shan Road SOHO, Sheung Wan, Hong Kong", phone: "983-284334", image: "petiteoyster.jpg", isVisited: false),
        Restaurant(name: "For Kee Restaurant", type: "Bakery", location: "Shop J-K., 200 Hollywood Road, SOHO, Sheung Wan, Hong Kong", phone: "232-434222", image: "forkeerestaurant.jpg", isVisited: false),
        Restaurant(name: "Po's Atelier", type: "Bakery", location: "G/F, 62 Po Hing Fong, Sheung Wan, Hong Kong", phone: "234-834322", image: "posatelier.jpg", isVisited: false),
        Restaurant(name: "Bourke Street Backery", type: "Chocolate", location: "633 Bourke St Sydney New South Wales 2010 Surry Hills", phone: "982-434343", image:"bourkestreetbakery.jpg", isVisited: false),
        Restaurant(name: "Haigh's Chocolate", type: "Cafe", location: "412-414 George St Sydney New South Wales", phone: "734-232323", image: "haighschocolate.jpg", isVisited: false),
        Restaurant(name: "Palomino Espresso", type: "American / Seafood", location: "Shop 1 61 York St Sydney New South Wales", phone: "872-734343", image: "palominoespresso.jpg", isVisited: false),
        Restaurant(name: "Upstate", type: "American", location: "95 1st Ave New York, NY 10003", phone: "343-233221", image: "upstate.jpg", isVisited: false),
        Restaurant(name: "Traif", type: "American", location: "229 S 4th St Brooklyn, NY 11211", phone: "985-723623", image: "traif.jpg", isVisited: false),
        Restaurant(name: "Graham Avenue Meats", type: "Breakfast & Brunch", location: "445 Graham Ave Brooklyn, NY 11211", phone: "455-232345", image: "grahamavenuemeats.jpg", isVisited: false),
        Restaurant(name: "Waffle & Wolf", type: "Coffee & Tea", location: "413 Graham Ave Brooklyn, NY 11211", phone: "434-232322", image: "wafflewolf.jpg", isVisited: false),
        Restaurant(name: "Five Leaves", type: "Coffee & Tea", location: "18 Bedford Ave Brooklyn, NY 11222", phone: "343-234553", image: "fiveleaves.jpg", isVisited: false),
        Restaurant(name: "Cafe Lore", type: "Latin American", location: "Sunset Park 4601 4th Ave Brooklyn, NY 11220", phone: "342-455433", image: "cafelore.jpg", isVisited: false),
        Restaurant(name: "Confessional", type: "Spanish", location: "308 E 6th St New York, NY 10003", phone: "643-332323", image: "confessional.jpg", isVisited: false),
        Restaurant(name: "Barrafina", type: "Spanish", location: "54 Frith Street London W1D 4SL United Kingdom", phone: "542-343434", image: "barrafina.jpg", isVisited: false),
        Restaurant(name: "Donostia", type: "Spanish", location: "10 Seymour Place London W1H 7ND United Kingdom", phone: "722-232323", image: "donostia.jpg", isVisited: false),
        Restaurant(name: "Royal Oak", type: "British", location: "2 Regency Street London SW1P 4BZ United Kingdom", phone: "343-988834", image: "royaloak.jpg", isVisited: false),
        Restaurant(name: "CASK Pub and Kitchen", type: "Thai", location: "22 Charlwood Street London SW1V 2DY Pimlico", phone: "432-344050", image: "caskpubkitchen.jpg", isVisited: false)
    ]

  • Restaurant添加屬性phone耻涛。
  • 修改RestaurantDetailViewController中兩個table view方法:
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! RestaurantDetailTableViewCell
        // Configure the cell...
        switch indexPath.row {
        case 0:
            cell.fieldLabel.text = "Name"
            cell.valueLabel.text = restaurant.name
        case 1:
            cell.fieldLabel.text = "Type"
            cell.valueLabel.text = restaurant.type
        case 2:
            cell.fieldLabel.text = "Location"
            cell.valueLabel.text = restaurant.location
        case 3:
            cell.fieldLabel.text = "Phone"
            cell.valueLabel.text = restaurant.phone
        case 4:
            cell.fieldLabel.text = "Been here"
            cell.valueLabel.text = (restaurant.isVisited) ? "Yes, I've been herebefore" : "No"
        default:
            cell.fieldLabel.text = ""
            cell.valueLabel.text = ""
        }
        
        cell.backgroundColor = UIColor.clear
        
        return cell
    }

使Cell自適應

  • RestaurantDetailViewController中的viewDidLoad中添加:
        tableView.estimatedRowHeight = 36.0    
        tableView.rowHeight = UITableViewAutomaticDimension
  • estimatedRowHeight是cell的預計高度优训,這邊就設置成原本prototype cell的高度呢燥。
  • UITableViewAutomaticDimension表示超過預計高度后自動適應高度搁吓。
  • 修改Value label的numberOfLines屬性為0原茅,就是不限制行數(shù)。

添加spacing約束

當value label超過兩行堕仔,文本顯示出問題:

這是因為雖然value label的高度自適應了合適的高度擂橘,但是stack view的高度沒有約束,不能計算的出來摩骨,因此要給stack view添加兩個上下的相等spcing約束通贞。


這樣不管value label是多少行,高度都會自適應了恼五。

Dynamic Type

Dynamic Type當在手機設置 >通用 > 輔助功能 > 更大字體設置字體大小時昌罩,app中的應用也相應的變化。
只要把字體設置成text style - Headline就會有Dynamic Type功能灾馒。目前只有name label的字體設置成了text style - Headline茎用,其他設置成固定大小的字體,不會隨著在手機設置 >通用 > 輔助功能 > 更大字體設置字體大小時而變化睬罗。

代碼

Beginning-iOS-Programming-with-Swift

說明

此文是學習appcode網(wǎng)站出的一本書 《Beginning iOS 10 Programming with Swift》 的一篇記錄

系列文章目錄

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市容达,隨后出現(xiàn)的幾起案子古涧,更是在濱河造成了極大的恐慌,老刑警劉巖花盐,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件羡滑,死亡現(xiàn)場離奇詭異,居然都是意外死亡算芯,警方通過查閱死者的電腦和手機柒昏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來也祠,“玉大人,你說我怎么就攤上這事近速≌┖伲” “怎么了堪旧?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長奖亚。 經(jīng)常有香客問我淳梦,道長,這世上最難降的妖魔是什么昔字? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任爆袍,我火速辦了婚禮,結果婚禮上作郭,老公的妹妹穿的比我還像新娘陨囊。我一直安慰自己,他們只是感情好夹攒,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布蜘醋。 她就那樣靜靜地躺著,像睡著了一般咏尝。 火紅的嫁衣襯著肌膚如雪压语。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天编检,我揣著相機與錄音胎食,去河邊找鬼。 笑死允懂,一個胖子當著我的面吹牛厕怜,可吹牛的內容都是我干的。 我是一名探鬼主播累驮,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼酣倾,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了谤专?” 一聲冷哼從身側響起躁锡,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎置侍,沒想到半個月后映之,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡蜡坊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年杠输,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片秕衙。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡蠢甲,死狀恐怖,靈堂內的尸體忽然破棺而出据忘,到底是詐尸還是另有隱情鹦牛,我是刑警寧澤搞糕,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站曼追,受9級特大地震影響窍仰,放射性物質發(fā)生泄漏。R本人自食惡果不足惜礼殊,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一驹吮、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧晶伦,春花似錦碟狞、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至近忙,卻和暖如春竭业,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背及舍。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工未辆, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人锯玛。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓咐柜,卻偏偏與公主長得像,于是被迫代替她去往敵國和親攘残。 傳聞我的和親對象是個殘疾皇子拙友,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內容