iOS项目开发实战——学会使用TableView列表控件(四)plist读取与Section显示
2023-09-11 14:20:43 时间
文本将会实现把数据存储到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 。欢迎大家訪问!
相关文章
- [ IOS ] iOS-控制器View的创建和生命周期
- IOS中弹出键盘后出现fixed失效现象的解决方案
- IOS 30多个iOS常用动画,带详细注释
- iOS开发UI篇—UITableview控件基本使用
- iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)
- uni-app:ios/android中的nvue和vue页面加载自定义字体(hbuilderx 3.7.3)
- 【IOS-COCOS2D游戏开发之十二】浅析使用C++/C/OC进行IOS游戏混编出现“FAILED WITH EXIT”问题与小结;
- 【Swift】iOS开发历险记(二)
- uniapp小程序 安卓和ios时间兼容性问题解决方案demo(整理)
- uniapp中IOS端小程序底部黑线适配的方法(整理)
- iOS 学习群
- iOS 内存布局 内存管理方案
- ios NSMutableArray removeObject
- iOS 实现QQ界面
- cordova与ios native code交互的原理