闲话不多久,这几天才开始上手swift,惭愧,惭愧!这个 demo我只实现了我个人项目的一个页面,用的MVC模式。字典转模型用的是马爸爸家的handyJson,真的很不错,
if let goods = JSONDeserializer<ALSGoods>.deserializeModelArrayFrom(json: json!, designatedPath:"content.productList.pageList") {
goods.forEach({ (good) in
if good != nil {
self.dataSource?.append(good!);
}
})
}
这个放的是模型,有没有感觉和MJExtension很像,required init() {}是需要在模型里面实现的方法
class ALSGoods: HandyJSON {
var joinTotal:String?
var lotteryId:String?
var picture:String?
var title:String?
required init() {}
}
有了数据,接下来我们就开创建cell了
static private let cellID = "ALSShoppingCartCell"
class func shoppingCartCellWithTableView(tableView: UITableView) -> ALSGoodsCell {
var cell = tableView.dequeueReusableCell(withIdentifier: cellID) as? ALSGoodsCell
if cell == nil {
cell = ALSGoodsCell(style: .default, reuseIdentifier: cellID)
}
return cell!
}
private override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupBase()
setupSubViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
开始给cell传数据,重写set方法
var goodsModels : Array<ALSGoods>! {
didSet{
for i in 0..<goodsModels.count {
let goodsView = self.goodsViews[i]
goodsView.goodsModel = goodsModels[i]
}
let goodsView = self.goodsViews.last!
if goodsModels.count == 1 {
goodsView.isHidden = true
}
else {
goodsView.isHidden = false
}
}
}
网友评论