2017-03-29 5 views
2

私は、スウィフトプレイグラウンドのiPadアプリでビューコントローラで作成したテーブルビューでカスタムセルを実装しようとしています。同様の問題を扱う多くのスタックオーバーフローポストを見たことがありますが、私の問題を解決する解決策を見つけることができませんでした。ここでUITableViewCellがデータを表示しない - Swift Playgrounds

は私CustomCellクラスです:

class CustomCell: UITableViewCell { 

    let projectImage = UIImageView() 
    //let projectDivider = UIView() 
    let projectTitle = UILabel() 

    override func awakeFromNib() { 
     projectImage.frame = CGRect(x: 0, y: 0, width: 100, height: 100) 
     //projectDivider.frame = CGRect(x: 100, y: 0, width: 20, height: 100) 
     projectTitle.frame = CGRect(x: 120, y: 0, width: 200, height: 100) 

     self.contentView.addSubview(projectImage) 
     //self.contentView.addSubview(projectDivider) 
     self.contentView.addSubview(projectTitle) 
    } 

} 

次は私のビューコントローラのコードです:

class AboutViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    let tableView = UITableView() 
    let cellReuseIdentifier: String = "cell" 

    var projectTitles = ["Madhubani Coffee", "World Savvy", "Zipshare", "CrashThatCode", "Future Plans"] 
    var images: [UIImage] = [UIImage(named: "IMG_0061.JPG")!, UIImage(named: "IMG_0064.JPG")!, UIImage(named: "IMG_0062.JPG")!, UIImage(named: "IMG_0062.JPG")!, UIImage(named: "IMG_0062.JPG")!] 

    override func viewDidLoad() { 
     view.backgroundColor = UIColor.white 

     tableView.frame = CGRect(x: -82.5, y: 250, width: 600, height: 600) 
     tableView.delegate = self 
     tableView.dataSource = self 
     tableView.backgroundColor = UIColor.white 

     self.tableView.register(CustomCell.self as AnyClass, forCellReuseIdentifier: cellReuseIdentifier) 
     view.addSubview(tableView) 

    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 5 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     var cell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as! CustomCell 

     if cell == nil { 
      cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellReuseIdentifier) as! CustomCell 
     } 

     cell.projectImage.image = images[indexPath.row] 
     cell.projectTitle.text = projectTitles[indexPath.row] 

     cell.clipsToBounds = true 

     return cell 
    } 
} 

私は、問題が何であるかを知りません。細胞は単に表示されないようです。どんな助けもありがとう!

答えて

4

あなたの問題は、あなたからawakeFromNibを行うにはnib(またはxib)ファイルを持っていないということです。

代わりにCustomCellのためにこれをやってみてください。

class CustomCell: UITableViewCell { 

    let projectImage = UIImageView() 
    //let projectDivider = UIView() 
    var projectTitle = UILabel() 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 

     projectImage.frame = CGRect(x: 0, y: 0, width: 100, height: 30) 
     //projectDivider.frame = CGRect(x: 100, y: 0, width: 20, height: 100) 
     projectTitle.frame = CGRect(x: 10, y: 0, width: 200, height: 30) 
     projectTitle.font = UIFont.systemFont(ofSize: 20.0) 
     //self.contentView.addSubview(projectImage) 
     //self.contentView.addSubview(projectDivider) 
     self.contentView.addSubview(projectTitle) 
    } 

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

} 
関連する問題