iOS项目开发实战——学会使用TableView列表控件(四)plist读取与Section显示

       文本将会实现把数据存储到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  。欢迎大家訪问!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值