Swift
// ViewController.swift
// Modal 模型处理
var myDataArray:NSMutableArray!
myDataArray = NSMutableArray()
for bookInfoDic in NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("bookData", ofType: "plist")!)! {
let bookModel = BookModel()
// KVC
bookModel.setValuesForKeysWithDictionary(bookInfoDic as! Dictionary)
// 使用 Modal 数据模型初始化数据源数组
myDataArray.addObject(bookModel)
}
// View 视图处理
let myTableView:UITableView = UITableView(frame: CGRectMake(0, 20, self.view.bounds.size.width,
self.view.bounds.size.height - 20))
myTableView.delegate = self
myTableView.dataSource = self
myTableView.registerNib(UINib(nibName: "BookCell", bundle: nil), forCellReuseIdentifier: "BookCell")
self.view.addSubview(myTableView)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myDataArray.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as! BookCell
let bookModel:BookModel = myDataArray.objectAtIndex(indexPath.row) as! BookModel
cell.configWithModel(bookModel)
// 从 Modal 数据模型中取出数据更新 View 的内容
return cell
}