iOS項目開發實戰——學會使用TableView列表控件(四)plist讀取與Section顯示
阿新 • • 發佈:2017-08-18
track arr 學會 有關 control label center head fcm
(3)最後的實現效果例如以下:
文本將會實現把數據存儲到plist文件裏。然後在程序中進行讀取。在TableView控件中依據不同的類別顯示Section。
有關TableView 的其它實現,請參考《iOS項目開發實戰——學會使用TableView列表控件(一)》《iOS項目開發實戰——學會使用TableView列表控件(二)》《iOS項目開發實戰——學會使用TableView列表控件(三)了解Section》。
(1)新建一個Property List文件,這個也就是plist文件。我取名為data.plist。輸入內容例如以下:
.
(2)代碼中實現例如以下:
import UIKit class ViewController: UIViewController ,UITableViewDataSource{ var data:NSDictionary! override func viewDidLoad() { super.viewDidLoad() data = NSDictionary(contentsOfURL: NSBundle.mainBundle().URLForResource("data", withExtension: "plist")!) } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell var title = cell.viewWithTag(101) as! UILabel title.text = (data.allValues[indexPath.section] as! NSArray).objectAtIndex(indexPath.row) as? String return cell } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return (data.allValues[section] as! NSArray).count } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return data.count } func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return data.allKeys[section] as? String } }
(3)最後的實現效果例如以下:
。
github主頁:https://github.com/chenyufeng1991 。
歡迎大家訪問。
iOS項目開發實戰——學會使用TableView列表控件(四)plist讀取與Section顯示