- 我是在項目中直接創(chuàng)建的plist文件,用于存儲 圖片和文字.格式如下:
Snip20170606_14.png
plist 文件格式如下:
Snip20170606_15.png
讀取plist文件代碼:
/// 讀取plist數(shù)據(jù)
func readPlist(fileName:String) -> [[String:String]]{ //
let list = Bundle.main.path(forResource: fileName, ofType: "plist")
let array = NSArray(contentsOfFile: list!)
return array as! [[String : String]]
}
model 格式如下:
class LeftViewModel: NSObject {
/// 圖片
var icon:String?
/// 名稱
var title:String?
let properties = ["icon","title"]
init(dict:[String:String]) {
super.init()
setValuesForKeys(dict)
}
class func dictToModel(list:[[String:String]]) -> [LeftViewModel]{
var models = [LeftViewModel]()
for dict in list {
models.append(LeftViewModel(dict: dict))
}
return models
}
override func setValue(_ value: Any?, forKey key: String) {
super.setValue(value, forKey: key)
}
override func setValue(_ value: Any?, forUndefinedKey key: String) {
}
override var description: String{
let dict = dictionaryWithValues(forKeys: properties)
return "\(dict)"
}
/// 獲取PList中數(shù)據(jù)
class func LeftData() -> [[String:String]]{
let array = Bundle.main.readPlist(fileName: "LeftViewData")
return array
}
}
tableView使用如下:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (dataList?.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = LJLeftViewCell.cell(tableView: tableView)
cell.title.text = dataList?[indexPath.row].title
// 圖片
let icon = dataList?[indexPath.row].icon
cell.icon.image = UIImage(named: icon!)
return cell
}
使用如下:
let leftData = LeftViewModel.LeftData()
let model = LeftViewModel.dictToModel(list: leftData)
打印結(jié)果:
Snip20170606_18.png
Snip20170606_17.png