2016-05-04 4 views
1

Webサーバーからデータをロードしていますが、メンバーテーブルビューnumberOfRowsInSectionにあいまいな参照がありますか?以下は私の素早いファイルです。私はここで何が欠けていますか? ViewModelにはtableViewプロパティを公開するから継承物事のセクション内のメンバーテーブルビュー数の行へのあいまいな参照

TableViewController.swift

import UIKit 

    class TableViewController: UITableViewController { 
     var viewModel:ViewModel! 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      self.viewModel = ViewModel() 
      self.tableView.delegate = viewModel 
      self.tableView.dataSource = viewModel 

      self.tableView.registerNib(UINib(nibName: "TableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: tableViewCellIdentifier) 

      self.tableView.rowHeight = UITableViewAutomaticDimension 
      self.tableView.estimatedRowHeight = 50; 
     } 

     override func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
      // Dispose of any resources that can be recreated. 
     } 


    } 

ViewModel.swift

import UIKit 

class ViewModel: NSObject,UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource { 


    var dataArrayPosts: NSArray! 
    var posts:[Post] = [] 

    override init() { 
     super.init() 


     let myURL = NSURL(string: "mydomainURLhere"); 
     let requestPosts = NSMutableURLRequest(URL: myURL!); 
     requestPosts.HTTPMethod = "POST"; 

     let postStringVars = "" 


     requestPosts.HTTPBody = postStringVars.dataUsingEncoding(NSUTF8StringEncoding); 

     let taskPosts = NSURLSession.sharedSession().dataTaskWithRequest(requestPosts){ data, response, error in 

      if error != nil{ 

       print("error\(error)"); 
       return; 
      } 

      do { 
       if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary { 
        //print(json) 

        if let category = json["Posts"] as? NSMutableArray{ 
         //print(category) 

         _ = category 

         self.dataArrayPosts = json["Posts"] as! NSMutableArray; 



         dispatch_async(dispatch_get_main_queue()) { 

          self.posts.removeAll() 


          for item in self.dataArrayPosts { 


           let post = Post(postDesc: (item["postDesc"] as? String)!,imageName: (item["imageName"] as? String)!,bName: (item["bName"] as? String)!,postDate: (item["postDate"] as? String)!,postShowsUntil:(item["postShowsUntil"] as? String)!) 

           self.posts.append(post) 
           // Ambiguous on reloadData??????? 
           self.tableView.reloadData() 

          } 
         } 
        }else{ 

         dispatch_async(dispatch_get_main_queue()) { 
          print("Nothing Was Found") 

         } 

        } 

       } 
      } catch let error as NSError { 

       print(error.localizedDescription) 
      } 




     } 

     taskPosts.resume() 




    } 




    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return posts.count 

    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(tableViewCellIdentifier, forIndexPath: indexPath) as! TableViewCell 

     let post = posts[indexPath.row] 
     cell.quoteTextLabel.text = post.postDesc 
     cell.nameLabel.text = post.bName 

     if let imageName = post.imageName where !imageName.isEmpty{ 
      cell.photoView?.image = UIImage(named: imageName) 
      cell.photoWidthConstraint.constant = kDefaultPhotoWidth 
      cell.photoRightMarginConstraint.constant = kDefaultPhotoRightMargin 
     } 
     else { 
      cell.photoView?.image = nil 
      cell.photoWidthConstraint.constant = 0 
      cell.photoRightMarginConstraint.constant = 0 
     } 

     cell.contentView.setNeedsLayout() 
     cell.contentView.layoutIfNeeded() 

     return cell 
    } 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return posts.count 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewCellIdentifier, forIndexPath: indexPath) as! CollectionViewCell 

     return cell 
    } 







} 

enter image description here

+1

あなたが提供したコードは 'UITableViewController'ではありません。あなたは 'UITableView'オブジェクトへのリファレンスやプロパティを持っていません。 – JAL

+0

申し訳ありません...含まれる編集を参照してください。TableViewController.swiftとViewModel.swfit – user520300

+0

このコードのどの行がエラーを報告していますか? –

答えて

0

なし、あなたのViewModelはあなたのUITableViewControllerへの参照を必要としません。 ViewControllerがViewModelを所有しているので、次のような弱い参照でなければなりません。

class ViewModel: NSObject,UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource { 

    weak let tableViewController:UITableViewController 

    var dataArrayPosts: NSArray! 
    var posts:[Post] = [] 

    override init(controller:UITableViewController) { 
     self.tableViewController = controller 
     super.init() 
     // ... 
     self.tableViewController.tableView.reloadData() 
} 

class TableViewController: UITableViewController { 
    var viewModel:ViewModel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.viewModel = ViewModel(controller:self) 
     // ... 
}