tabelViewCell的定制

UITableViweCell的定制(手寫)

 //MARK: - 屬性
    //1.tableView
    let tableView = UITableView.init(frame: UIScreen.mainScreen().bounds, style: .Plain)
    //2.數(shù)據(jù)源數(shù)組
    lazy var dataArray:[NSDictionary] = {
    
        var tempArray = [NSDictionary]()
        
        //1.獲取plist文件的路徑
        let path = NSBundle.mainBundle().pathForResource("data2.plist", ofType: nil)
        //2.拿到plist文件最外層的數(shù)組
        //將plist文件轉(zhuǎn)換成OC的數(shù)組(前提是plist的最外層是數(shù)組)
        //參數(shù):plist文件的路徑
        let plistArray = NSArray.init(contentsOfFile: path!)
        
        //3.遍歷數(shù)組拿到里面的字典
        for item in plistArray!{
        
            let dict = item as! NSDictionary
            
            //將字典存到數(shù)據(jù)源數(shù)組中
            tempArray.append(dict)
            
        }
        
        return tempArray
    }()
    
    
    

    //MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        //1.添加到界面上
        self.view.addSubview(self.tableView)
        //2.設(shè)置代理
        self.tableView.dataSource = self
        self.tableView.delegate = self
        //3.設(shè)置行高
        self.tableView.rowHeight = 225
    }
}

//MARK: - tableView DataSource
extension ViewController: UITableViewDataSource{

    //1.設(shè)置每個分組的cell的個數(shù)
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArray.count
    }
    
    //2.創(chuàng)建celll
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //1.去復(fù)用池中查找可以復(fù)用的cell
        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? ManTableViewCell
        
        //2.判斷是否找到可以復(fù)用的cell
        if cell == nil {
            
            cell = ManTableViewCell.init(style: .Subtitle, reuseIdentifier: "cell")
        }
        
        //3.刷新數(shù)據(jù)
        //a.拿到當(dāng)前cell對應(yīng)字典
        let dict = self.dataArray[indexPath.row]
        //b.取到圖片二進制
        let data = dict["image_data"] as! NSData
        //將二進制轉(zhuǎn)換成圖片
        //封面
        cell?.coverImageView.image = UIImage.init(data: data)
        //標(biāo)題
        cell?.titleLabel.text = dict["title"] as? String
        //作者頭像
        let data2 = dict["autorIcon_data"] as! NSData
        cell?.iconButton.setImage(UIImage.init(data: data2), forState: .Normal)
        //作者名
        cell?.authorNameLabel.text = dict["author_name"] as? String
        
        
        //4.返回cell
        return cell!
        
        
    }

自己定制的tableViewCell


//定制cell的步驟:
//1.創(chuàng)建一個類繼承自UITableViewCell
//2.聲明cell上所有的子視圖對應(yīng)的屬性
//3.在構(gòu)造方法中去添加子視圖(不需要設(shè)置frame)。注意:如果想要將子視圖直接添加到cell上,不能通過cell去調(diào)用addSubView方法饺谬,而是用cell的contentView去調(diào)用addSubView方法
//4.在layoutSubViews方法中去設(shè)置子視圖的frame


//1.封面
//2.頭像
//3.作者名
//4.標(biāo)題
//5.透明層
class ManTableViewCell: UITableViewCell {

    //MARK: - 第二步,聲明屬性
    //1.封面
    let coverImageView = UIImageView()
    //2.頭像
    let iconButton = UIButton()
    //3.作者名
    let authorNameLabel = UILabel()
    //4.標(biāo)題
    let titleLabel = UILabel()
    //5.透明層
    let alphaView = UIView()
    
    //MARK: - 第三步,重寫構(gòu)造方法可很,添加子視圖
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //1.封面
        self.contentView.addSubview(coverImageView)
        //2.頭像
        self.contentView.addSubview(iconButton)
        //3.作者名
        self.contentView.addSubview(authorNameLabel)
        self.authorNameLabel.textColor = UIColor.whiteColor()
        //4.標(biāo)題
        self.contentView.addSubview(titleLabel)
        self.titleLabel.textColor = UIColor.whiteColor()
        //5.透明層
        self.contentView.addSubview(alphaView)
        self.alphaView.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}


extension ManTableViewCell{

    //MARK: - 第四步办成,計算子視圖的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //通用
        let cellW = self.frame.size.width
        let cellH = self.frame.size.height
        let margin:CGFloat = 20

        //1.封面
        let coverX: CGFloat = 0
        let coverY: CGFloat = 0
        let coverW = cellW
        let coverH = cellH
        self.coverImageView.frame = CGRectMake(coverX, coverY, coverW, coverH)
        //2.頭像
        let iconX = margin
        let iconY = margin
        let iconW: CGFloat = 80
        let iconH: CGFloat = 80
        self.iconButton.frame = CGRectMake(iconX, iconY, iconW, iconH)
        //切圓
        iconButton.layer.masksToBounds = true
        iconButton.layer.cornerRadius = 40
        
        
        //3.作者名
        let authorX = iconX + iconW + margin
        let authorH: CGFloat = 20
        let authorY = iconY + iconH/2 - authorH/2
        let authorW = cellW - authorX - margin
        authorNameLabel.frame = CGRectMake(authorX, authorY, authorW, authorH)
        //4.標(biāo)題
        let titleX = margin
        let titleH: CGFloat = 20
        let titleY = cellH - margin - titleH
        let titleW = cellW - margin*2
        titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH)
        //5.透明層
        let alphaX:CGFloat = 0
        let alphaH = margin + titleH + margin
        let alphaY = cellH - alphaH
        let alphaW = cellW
        alphaView.frame = CGRectMake(alphaX,alphaY, alphaW, alphaH)
        
    }
}

重點


 //MARK: - 屬性
    //1.數(shù)據(jù)源數(shù)組
    lazy var dataArray:[DataModel] = {
    
        return self.getData()
    }()

    //2.tableView
    let tableView = UITableView.init(frame: CGRectZero, style: .Plain)
    
    //3.存儲frameModel的數(shù)組
    var frameArray = [FrameModel]()
    
    
    //MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        //1.設(shè)置frame
        self.tableView.frame = self.view.bounds
        //2.添加到界面上
        self.view.addSubview(self.tableView)
        //3.設(shè)置代理
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }

}

//MARK: - tableView Delegate
extension ViewController:UITableViewDelegate{

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        let frameModel = self.frameArray[indexPath.row]
        
        return frameModel.cellHeight
    }
}

//MARK: - tableView DataSource
extension ViewController:UITableViewDataSource{
    
    //1.設(shè)置每個分組cell的個數(shù)
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArray.count
    }
    
    //2.創(chuàng)建cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? MyTableViewCell
        
        if cell == nil {
            
            cell = MyTableViewCell.init(style: .Subtitle, reuseIdentifier: "cell")
            //取消選中效果
            cell?.selectionStyle = .None
            
        }
        
        //刷新數(shù)據(jù)
        let model = self.dataArray[indexPath.row]
        let frameModel = self.frameArray[indexPath.row]

        cell?.model = model
        cell?.frameModel = frameModel
        
        
        
        return cell!
    }

}



//MARK: - 獲取數(shù)據(jù)
extension ViewController{

    func getData() -> [DataModel]{
        var tempArray = [DataModel]()
        
        //1.拿到plist文件的路徑
        let path = NSBundle.mainBundle().pathForResource("statuses.plist", ofType: nil)
        //2.獲取plist文件中的數(shù)組
        let plistArray = NSArray.init(contentsOfFile: path!)
        //3.遍歷數(shù)組赦政,拿到里面的字典
        for item in plistArray! {
            let dict  = item as! NSDictionary
            
            //將字典轉(zhuǎn)換成對應(yīng)的數(shù)據(jù)模型
            let model = DataModel(dict: dict)
            
            //根據(jù)數(shù)據(jù)模型創(chuàng)建frameModel
            let frameModel = FrameModel(model: model)
            
            //將數(shù)據(jù)模型存到數(shù)據(jù)源數(shù)組中
            tempArray.append(model)
            //將frameModel存到數(shù)組中
            self.frameArray.append(frameModel)
        }
        
        
        
        return tempArray
    }
    
}


DataModel

//數(shù)據(jù)模型類:專門負(fù)責(zé)數(shù)據(jù)的存儲和操作
class DataModel: NSObject {

    //MARK: - 屬性
    //1.文字
    var text = ""
    //2.頭像
    var icon = ""
    //3.用戶名
    var name = ""
    //4.是否是VIP
    var vip = 0
    //5.圖片(這個屬性可能沒有)
    var picture:String? = nil

    //MARK: - 構(gòu)造方法
    //通過制定的字典創(chuàng)建模型
    init(dict:NSDictionary) {
        
        self.text = dict["text"] as! String
        self.icon = dict["icon"] as! String
        self.name = dict["name"] as! String
        self.vip = dict["vip"] as! Int
        self.picture = dict["picture"] as? String
    }
}

FrameModel


//數(shù)據(jù)模型類:專門負(fù)責(zé)數(shù)據(jù)的存儲和操作
class DataModel: NSObject {

    //MARK: - 屬性
    //1.文字
    var text = ""
    //2.頭像
    var icon = ""
    //3.用戶名
    var name = ""
    //4.是否是VIP
    var vip = 0
    //5.圖片(這個屬性可能沒有)
    var picture:String? = nil

    //MARK: - 構(gòu)造方法
    //通過制定的字典創(chuàng)建模型
    init(dict:NSDictionary) {
        
        self.text = dict["text"] as! String
        self.icon = dict["icon"] as! String
        self.name = dict["name"] as! String
        self.vip = dict["vip"] as! Int
        self.picture = dict["picture"] as? String
    }
}

定制的tableView


//1.聲明所需要的所有的子視圖對應(yīng)的屬性
//2.將子視圖添加到cell上
//3.計算frame


//1.頭像
//2.名字
//3.vip
//4.文字
//5.圖片
class MyTableViewCell: UITableViewCell {
    //MARK: - 屬性
    //1.頭像
    let iconImageView = UIImageView()
    //2.名字
    let nameLabel = UILabel()
    //3.vip
    let vipImageView = UIImageView()
    //4.文字
    let contentLabel = UILabel()
    //5.圖片
    let pictureImageView = UIImageView()
    
    //MARK: - 在給cell的模型賦值的時候去設(shè)置子視圖的屬性
    //6.獲取模型的值
    var frameModel: FrameModel? = nil
    
    var model: DataModel? = nil{
    
        didSet{
        
            //1.頭像
            self.iconImageView.image = UIImage.init(named: (model?.icon)!)
            //2.名字
            self.nameLabel.text = model?.name
            //3.vip
            //注意:在cell中如果出現(xiàn)if語句粱甫,必須把對應(yīng)的else的情況描述清除
            if model?.vip == 1 {
                
                self.vipImageView.hidden = false
                self.nameLabel.textColor = UIColor.redColor()
            }else{
                
                self.vipImageView.hidden = true
                self.nameLabel.textColor = UIColor.blackColor()
            }
            //4.文字
            self.contentLabel.text = model?.text
            
            //5.圖片
            if model!.picture == nil {
                
                self.pictureImageView.hidden = true
                
            }else{
            
                self.pictureImageView.hidden = false
                let path = NSBundle.mainBundle().pathForResource(model?.picture, ofType: nil)
                self.pictureImageView.image = UIImage.init(contentsOfFile: path!)
            }
            
            
            
        }
    }
    
    
    //MARK: - 構(gòu)造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //1.頭像
        self.contentView.addSubview(iconImageView)
        //self.iconImageView.backgroundColor = UIColor.yellowColor()
        //2.名字
        self.contentView.addSubview(nameLabel)
        self.nameLabel.font = UIFont.systemFontOfSize(13)
        //self.nameLabel.backgroundColor = UIColor.greenColor()
        //3.vip 
      self.contentView.addSubview(vipImageView)
        vipImageView.image = UIImage.init(named: "vip")
        //4.文字
      self.contentView.addSubview(contentLabel)
        self.contentLabel.font = UIFont.systemFontOfSize(14)
        self.contentLabel.numberOfLines = 0
        
        //contentLabel.backgroundColor = UIColor.orangeColor()
        
        
        //5.圖片
        self.contentView.addSubview(pictureImageView)
        self.pictureImageView.backgroundColor = UIColor.purpleColor()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//MARK: - 計算frame
extension MyTableViewCell{

    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.iconImageView.frame = self.frameModel!.iconFrame
        self.nameLabel.frame = self.frameModel!.nameFrame
        self.vipImageView.frame = self.frameModel!.vipFrame
        self.contentLabel.frame = self.frameModel!.textFrame
        self.pictureImageView.frame = self.frameModel!.pictureFrame
        
    }
    
    
    /////============////
    
    
    ////=============////
    
    
}


//MARK: - 其他
extension MyTableViewCell{

    //在通過xib的方式創(chuàng)建cell的時候才會調(diào)用
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    //選中cell的時候會自動調(diào)用
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末泳叠,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子茶宵,更是在濱河造成了極大的恐慌危纫,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乌庶,死亡現(xiàn)場離奇詭異种蝶,居然都是意外死亡,警方通過查閱死者的電腦和手機瞒大,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門螃征,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人糠赦,你說我怎么就攤上這事会傲。” “怎么了拙泽?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵淌山,是天一觀的道長。 經(jīng)常有香客問我顾瞻,道長泼疑,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任荷荤,我火速辦了婚禮退渗,結(jié)果婚禮上移稳,老公的妹妹穿的比我還像新娘。我一直安慰自己会油,他們只是感情好个粱,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著翻翩,像睡著了一般都许。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嫂冻,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天胶征,我揣著相機與錄音,去河邊找鬼桨仿。 笑死睛低,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的服傍。 我是一名探鬼主播钱雷,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼伴嗡!你這毒婦竟也來了急波?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤瘪校,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后名段,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體阱扬,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年伸辟,在試婚紗的時候發(fā)現(xiàn)自己被綠了麻惶。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡信夫,死狀恐怖窃蹋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情静稻,我是刑警寧澤警没,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站振湾,受9級特大地震影響杀迹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜押搪,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一树酪、第九天 我趴在偏房一處隱蔽的房頂上張望浅碾。 院中可真熱鬧,春花似錦续语、人聲如沸垂谢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽滥朱。三九已至,卻和暖如春娃豹,著一層夾襖步出監(jiān)牢的瞬間焚虱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工懂版, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留鹃栽,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓躯畴,卻偏偏與公主長得像民鼓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蓬抄,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容