nibを使ってTableを作っているのですが、どうもTableにうまくデータが入れられません。
ログを取ってみると、tableは生成されているものの、numberOfRowsInSectionのほうが実行されていません。
なので当然、cellForRowAtIndexPathのほうのmethodも実行されておらず、CellInfoなどの方も実行されていません。
別のControllerで同じように実装して動いているので、どこかに見落としているミスがあるか、nibの問題(AutoLayoutなど)かと思ったのですが、どちらもミスが見つけれません。。
1.MainViewController.swift
Swift
1class MainViewController: UIViewController, UITableViewDelegate, UIWebViewDelegate, UINavigationControllerDelegate{ 2 3 @IBOutlet weak var projectTableView: UITableView! 4 5 var originalData: [[String: AnyObject]]? 6 var tableData: [MainViewCellInfo] = [] 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 //xibを登録 12 let nib = UINib(nibName: MainViewTableCell.nibName(), bundle: nil) 13 self.projectTableView.registerNib(nib, forCellReuseIdentifier: MainViewTableCell.cellId()) 14 } 15 16 //プロジェクト一覧を取得 17 func onGetProjects(data: NSData?, res: NSURLResponse?, error: NSError?) { 18 19 Alamofire.request(.GET, "http://teratail.com/api/v1/questions.json") 20 .responseJSON { response in 21 22 if response.result.isSuccess{ 23 self.setupData() 24 } 25 } 26 } 27 28 private func setupData() { 29 for oneData in self.originalData! { 30 let cellInfo = MainViewCellInfo(cellData: oneData) 31 self.tableData.append(cellInfo) 32 } 33 } 34 35} 36 37extension MainViewController: UITableViewDataSource { 38 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 39 40 return self.tableData.count 41 42 } 43 44 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 45 46 let cell = tableView.dequeueReusableCellWithIdentifier(MainViewTableCell.cellId(), forIndexPath: indexPath) as! MainViewTableCell 47 let cellInfo = self.tableData[indexPath.row] 48 cell.setupWith(cellInfo: cellInfo) 49 return cell 50 } 51}
2.MainViewTableCell.swift
Swift
1class MainViewTableCell: UITableViewCell{ 2 3 @IBOutlet weak var projectNameLabel: UILabel! 4 @IBOutlet weak var statusLabel: UILabel! 5 6 class func nibName() -> String { 7 // クラス名と nib 名が同じ前提 8 return String(self) 9 } 10 11 class func cellId() -> String { 12 return String(self) 13 } 14 15 func setupWith(cellInfo cellInfo_: MainViewCellInfo) { 16 self.projectNameLabel.text = cellInfo_.projectName 17 self.statusLabel.text = cellInfo_.status == true ? "状態1" : "状態2" 18 } 19 20}
3.MainViewCellInfo.swift
Swift
1class MainViewCellInfo { 2 3 private(set) var projectName: String! 4 private(set) var status: Bool! 5 6 init(cellData cellData_: [String: AnyObject]){ 7 8 projectName = cellData_["name"] as! String 9 10 } 11} 12 13
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/03 09:52