文本将会实现把数据存储到plist文件里。然后在程序中进行读取,在TableView控件中依据不同的类别显示Section。有关TableView 的其它实现,请參考《》《》《》。
(1)新建一个Property List文件。这个也就是plist文件。我取名为data.plist。
输入内容例如以下:
.
(2)代码中实现例如以下:
import UIKitclass 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主页: 。欢迎大家訪问!