- 我是在项目中直接创建的plist文件,用于存储 图片和文字.格式如下:
plist 文件格式如下:
读取plist文件代码:
/// 读取plist数据
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中数据
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)
打印结果: