国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

SwiftTableView

2019-11-14 20:21:54
字體:
來源:轉載
供稿:網友

 

 

代碼來源  cocoachina推薦源碼  26日

 

 

上面這些是一些基本的設置,然后提前補充幾個知識點!

 

類后面的!作用是強制類型轉換

NSCoder是一個抽象類,是字節流的抽象類,我們可以把數據寫入一個coder也可以從coder中讀出數據!

as也可以類型為類型轉換

Swift中sort函數有兩種用法,在編譯器中輸入sort查看幫助文檔有相信解釋!

建議觀看Swift language 函數章節

 

 

import UIKitclass ViewController: UITableViewController {        var familyNames : Array<String> = []    var fonts : Dictionary<String, String[]> = [:]        init(coder aDecoder: NSCoder!)    {        super.init(coder: aDecoder)                let unsortedFamilyNames = UIFont.familyNames() as String[]        familyNames = sort(unsortedFamilyNames)                for familyName in familyNames        {            let unsortedFontNames = UIFont.fontNamesForFamilyName(familyName) as String[]            fonts[familyName] = sortFontNames(unsortedFontNames)        }    }        override func numberOfSectionsInTableView(tableView: UITableView!) -> Int    {        return countElements(familyNames)    }        override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int    {        let key = familyNames[section]        let array = fonts[key]        return array!.count    }        override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!    {        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell                let key = familyNames[indexPath.section]        let array = fonts[key]        let fontName = array![indexPath.row]                cell.textLabel.text = fontName        cell.textLabel.font = UIFont(name:fontName, size: UIFont.systemFontSize())                return cell    }        override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat    {        let key = familyNames[indexPath.section]        let array = fonts[key]        let fontName = array![indexPath.row]                let label = UILabel(frame: CGRectMake(0, 0, 280, 200))        label.text = fontName        label.font = UIFont(name: fontName, size: UIFont.systemFontSize())        label.sizeToFit()                return max(label.font.lineHeight + label.font.ascender + -label.font.descender, 44)    }        /* This function is necessary because fonts shouldn't always be sorted alphabetically.    For example, ArialMT should come before Arial-BoldItalicMT,    but if we sort alphabetically, it doesn't. */    /* 這個函數是必要的,因為字體不總是按字母順序排序。    例如,ArialMT應該Arial-BoldItalicMT之前,    但是如果我們按字母順序排序,ArialMT就不會在Arial-BoldItalicMT之前。*/    func sortFontNames(array: String[]) -> String[]    {        return sort(array, { (s1: String, s2: String) -> Bool in            // if s1 doesn't contain a hyphen, it should appear before s2            let count1 = countElements(s1.componentsSeparatedByString("-"))            if count1 == 1            {                return true            }                        // if s2 doesn't contain a hyphen, it should appear before s1            let count2 = countElements(s2.componentsSeparatedByString("-"))            if count2 == 1            {                return false            }                        // otherwise, a normal string compare will be fine            return s1 > s2            })    }}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 紫阳县| 广水市| 博白县| 五家渠市| 静宁县| 青铜峡市| 礼泉县| 白银市| 项城市| 宜春市| 靖边县| 穆棱市| 宝丰县| 湄潭县| 平阳县| 海安县| 大安市| 镇赉县| 扶风县| 绥芬河市| 文登市| 肇东市| 乌海市| 汉中市| 广德县| 民勤县| 平泉县| 扶风县| 隆林| 永宁县| 乌拉特后旗| 永年县| 景东| 柘城县| 绥芬河市| 陇西县| 苍梧县| 绥化市| 左云县| 商丘市| 利川市|