IOS project development practice-learn to use the TableView list control (4) plist reading and Section display
The text will store the data in the plist file and then read it in the program. The Section is displayed in the TableView control according to different categories. For other implementations of TableView, see iOS project development practices-learn to use TableView list controls (1), iOS project development practices-learn to use TableView list controls (2) iOS project development practice-learn to use the TableView list control (3) understand Section.
(1) create a Property List file, which is the plist file. I named data. plist. The input content is as follows:
.
(2) The implementation in the Code is as follows:
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) The final implementation result is as follows: