iOS項目開發實戰——學會使用TableView清單控制項(四)plist讀取與Section顯示

來源:互聯網
上載者:User

iOS項目開發實戰——學會使用TableView清單控制項(四)plist讀取與Section顯示

文本將會實現把資料存放區到plist檔案中,然後在程式中進行讀取,在TableView控制項中根據不同的類別顯示Section。有關TableView 的其他實現,請參考《iOS項目開發實戰——學會使用TableView清單控制項(一)》《iOS項目開發實戰——學會使用TableView清單控制項(二)》《iOS項目開發實戰——學會使用TableView清單控制項(三)瞭解Section》。

(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)最後的實現效果如下:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.