Swift3.0 自定義tableView復用cell 的寫法,與CollectionViewCell的不同,數據model


 Model數據

class HospitalModel: NSObject {
    //后邊不賦值 會報錯
    var imgurl :String = ""
    var introduction : String = ""
    var kind :String = ""
    var lat: Float = 0
    var lng : Float = 0
    var medinsurance : Int = 0
    var name : String = ""
    var org_code : String = ""
    var recommend : Int = 0
    var regaddr : String = ""
    var regregion : Int = 0
    var linked : Int  = 0//連接狀態 0
    
    //當模型數據 少於網絡的時候
//    override func setValue(value:AnyObject?, forUndefinedKey key:String) {
//    }
}
Model

 

一、tableViewCell

1.聲明注冊 cell, 選擇要注冊的是class還是nib

**********快捷鍵:      Command+Shift+4      截取屏並自動保存在桌面***********

cell的寫法是 Cell名稱.self

 tableview.register(homecellTable.self, forCellReuseIdentifier: "cell")

 

2、在cellForRow中      判斷cell是否為空的方法 這個可行,有其他的歡迎留言

  var cell : homecellTable = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! homecellTable
        
        if cell.isEqual(nil) {
            
            cell = homecellTable(style: .default, reuseIdentifier: "cell")
            
        }

 

 

3、自定義cell布局

class homeYiyuancellTable: UITableViewCell {
    
    var imageyiyuan = UIImageView()
    var labelyiyuan = UILabel()
   
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = UIColor.white
 
        //此處開始布局
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
cell寫法

 

 

二、CollectionViewCell 的寫法

1、首先 cell的自定義上的不同

class homeYiyuancell: UICollectionViewCell {
    
    var imageyiyuan = UIImageView()
    var labelyiyuan = UILabel()
  
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.white
       //開始布局
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    
}
cell 寫法

 

2、注冊

//zhuce
        collectionVieww?.register(hometopCell.self, forCellWithReuseIdentifier: "topcell")

 

3、cellForRow

 let cell = collectionVieww?.dequeueReusableCell(withReuseIdentifier: "tuwen", for: indexPath) as! hometuwenCollectionViewCell

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM