Swift教程のTableViewは03カスタムCellを使います.

1857 ワード

Swift教程のTableViewは03カスタムCellを使います.
  • をクリックしてください.無料でSwiftを勉強してドルを稼ぎます.
    前シリーズのレッスン
  • Swift教程のTablewiwは01基礎コード
  • を使っています.
  • Swift教程のテーブルビューは02を使って画像を表示します.
    Swift教程のTableViewは03カスタムCellを使います.
    カスタムCellはswift apを開発する基本的な仕事であり、カスタムセルの技術を身につけてこそ、甲のいかなる態度表明要求を恐れないことができます.次はカスタムセルの基礎過程です.
    1.swiftを新たに作って、Card Cellと名づけます.
    コードは以下の通りです
    class CardCell:UITableViewCell {
        var cellData:CellData! {
            didSet{
                textLabel?.text = cellData.title
                imageView?.image = cellData.featureImage
            }
        }
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            
            contentView.backgroundColor = .yellow
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    その中に次の言葉があります.
    var cellData:CellData! {
           didSet{
               textLabel?.text = cellData.title
               imageView?.image = cellData.featureImage
           }
       }
    
    2.テーブルビューを修正する
    レジスターを修正する
     fileprivate func setupTableView() {
            tableView.register(CardCell.self, forCellReuseIdentifier: CELL_ID)
        }
    
    row生成を修正
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath) as!CardCell
            let section=sections[indexPath.section]
            let cellData=section.data[indexPath.row]
            cell.cellData=cellData
            r
    
    完了
    didSet関数で思う存分遊ぶことができます.
    var cellData:CellData! {
           didSet{
               textLabel?.text = cellData.title
               imageView?.image = cellData.featureImage
           }
       }