天天看點

swift 裡面tableview的cell代碼建立方法

網上搜了好多,都是xib拖得,手寫的幾乎沒有,作為一個手寫黨,不能忍受啊!于是乎,寫了一個自認為可以的建立方法 。求各位swift大神指點

(Xcode版本6.4)

代碼:在tableview裡面的使用

import UIKit

class CustomCellTestController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView:UITableView!
    var dataArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "代碼自定義cell"
        
        self.initDatas()
        self.view.backgroundColor = UIColor.whiteColor()
        
        self.tableView = UITableView(frame: CGRectMake(0.0, 0.0, KLScreenWidth, KLScreenHeight), style: UITableViewStyle.Plain)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.tableFooterView = UIView()
        self.tableView.rowHeight = 80
        
        // 注冊Cell
        self.tableView.registerClass(CustomWriteCell.classForCoder(), forCellReuseIdentifier: CustomWriteCell.cellID())
        
        self.view.addSubview(self.tableView)
    }
    // 初始化資料
    func initDatas(){
        
        let array:NSArray = ["要是能重來 我要選李白 幾百年前做的好壞 沒那麼多人猜",
            "要是能重來 我要選李白 至少我還能寫寫詩來澎湃 逗逗女孩",
            "要是能重來 我要選李白 創作也能到那麼高端 被那麼多人崇拜",
            "一天宛如一年 一年宛如一天 任時光流轉 我還是我",
            "一遍用了千遍 千遍隻為一遍 當回憶久遠 初心始現",
            "一天宛如一年 一年宛如一天 任時光流轉 我還是我 一遍用了千遍 千遍隻為一遍 當回憶久遠 初心始現 我做了那麼多改變 隻是為了我心中不變 默默地深愛着你無論相見不相見 我做了那麼多改變 隻是為了我心中不變 我多想你看見",
            "《李白》 AND 《我變了 我沒變》"]
        
        for var i = 0; i < array.count; i++ {
            let  tempModel:TestModel = TestModel()
            tempModel.name = (array[i] as? String)!
            tempModel.headUrl = "IMG_0542.jpg"
            self.dataArray .addObject(tempModel)
        }
    }
    
    // 傳回的行數
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    // 重用cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let  cell = tableView.dequeueReusableCellWithIdentifier(CustomWriteCell.cellID(), forIndexPath: indexPath) as! CustomWriteCell
        let model:TestModel = (self.dataArray[indexPath.row] as? TestModel)!
        cell.cellForModel(model)
        
        return cell
    }
    
    // 傳回cell高度 (高度固定時,不用這樣寫)
//    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
//        return CustomWriteCell.cellHeight()
//    }
    
    // 選中cell時的處理
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        println("點選的是第\(indexPath.row)行")
    }

    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

}
           

代碼:cell的建立

//  代碼自定義Cell

import UIKit

private let  KLMargin:CGFloat  = 10
private let  imgSize:CGFloat = 60
private let  titleWidth:CGFloat = KLScreenWidth - 3.0*KLMargin - imgSize

class CustomWriteCell: UITableViewCell {

    var  titleLabel : UILabel!
    var  headImageView : UIImageView!
    
    // 初始化cell
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: UITableViewCellStyle.Default, reuseIdentifier: CustomWriteCell.cellID())
        
        // 頭像img
        headImageView = UIImageView(frame: CGRectMake(KLMargin, KLMargin, imgSize, imgSize))
        self.contentView.addSubview(headImageView)
         <pre name="code" class="objc">        // 内容
        titleLabel = UILabel(frame: CGRectMake(CGRectGetMaxY(headImageView.frame) + KLMargin, KLMargin, titleWidth, imgSize))
        titleLabel.font = UIFont.systemFontOfSize(15)
        titleLabel.numberOfLines = 0
        self.contentView.addSubview(titleLabel)

    }
    
    // 類方法 重用辨別符
    class func cellID () -> String {
        return "CustomWriteCell"
    }
    
    // 類方法 傳回高度
    class func cellHeight() -> CGFloat {
    
        return 80
    }
    
    // 根據model 填充Cell
    func cellForModel(model: TestModel?){
        if let tempModel = model {
          
            titleLabel.text = tempModel.name
            headImageView.image = UIImage(named: tempModel.headUrl!);
        }
    }
    

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
           

代碼:簡單的model

//  model類

import UIKit

class TestModel: NSObject {
   
    // 目前測試 兩種寫法都行 第二種用的時候不要忘記解包(!)
    var  name = String()
    var  headUrl: String?
}
效果如圖:
           
swift 裡面tableview的cell代碼建立方法