在使用TableView设置Cell数据时,如果需要根据条件判断赋值,避免每一次设置都进行判断,消耗CPU,可以将判断的操作放在模型中进行
以新浪微博公共消息为例(存放公共微博信息的模型(原创微博))
class JSUserInfoModel: NSObject {
//微博ID
var id: Int = 0
//用户昵称
var screen_name: String?
//友好显示名称
var name: String?
//用户头像地址(中图),50×50像素
var profile_image_url: String?
// 是否是微博认证用户,即加V用户,true:是,false:否
var verified: Int = 0
// 认证类型,-1:没有认证,0,认证用户,2,3,5: 企业认证,220: 达人
var verified_type: Int = 0{
didSet{
verified_type_img = setImg_Verified_type(verified_type)
}
}
// 会员等级 0-6
var mbrank: Int = 0 {
didSet{
mbrank_img = setImg_Mbrank(mbrank)
}
}
var verified_type_img: UIImage?
var mbrank_img: UIImage?
//设置会员等级图片
private func setImg_Mbrank (mbrank: Int) -> UIImage? {
if mbrank > 0 && mbrank < 7 {
return UIImage(named: "common_icon_membership_level\(mbrank)")
}else {
return UIImage(named: "common_icon_membership_expired")
}
}
//设置认证图片
private func setImg_Verified_type (verified_type: Int) -> UIImage? {
switch verified_type {
case 0:
return UIImage(named: "avatar_vip")
case 2,3,5:
return UIImage(named: "avatar_enterprise_vip")
case 220:
return UIImage(named: "avatar_grassroot")
default:
return nil
}
}
init(dict: [String: AnyObject]) {
super.init()
setValuesForKeysWithDictionary(dict)
}
override func setValue(value: AnyObject?, forUndefinedKey key: String) {
}
}