iOS项目开发实战——学会使用TableView列表控件(四)plist读取与Section显示
2024-10-18 23:33:57
文本将会实现把数据存储到plist文件里。然后在程序中进行读取。在TableView控件中依据不同的类别显示Section。
有关TableView 的其它实现,请參考《iOS项目开发实战——学会使用TableView列表控件(一)》《iOS项目开发实战——学会使用TableView列表控件(二)》《iOS项目开发实战——学会使用TableView列表控件(三)了解Section》。
(1)新建一个Property List文件,这个也就是plist文件。我取名为data.plist。输入内容例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">.
(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)最后的实现效果例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">。
github主页:https://github.com/chenyufeng1991 。
欢迎大家訪问。
最新文章
- 【面试题】D
- ZOJ 3696 Alien's Organ
- STM32F105解密STM32F105VB芯片解密STM32F105R8单片机破解多少钱?
- Python 对目录中的文件进行批量转码(GBK>;UTF8)
- JUnit3的作用
- PL/SQL Developer 9.x 注册码
- Dictionary和IDictionary
- HBase(八): 表结构设计优化
- Parallel WebDriver executions using TestNG
- 阿里云服务器(Win 2008 R2 Standard)安装MSSM 2008 R2之1033和2052问题
- linux下mysql配置文件my.cnf详解
- Hadoop 学习笔记(一) HDFS API
- Visual Studio 2013 上使用 Github
- 数论+dp Codeforces Beta Round #2 B
- rgba()和opacity的使用
- 从LINQ开始之LINQ to Objects(下)
- 对于php内存释放问题
- Linux IO模型(同步异步阻塞非阻塞等)的几篇好文章
- 使用redis作为Return存储方式
- python中print和input的底层实现