对部分中的成员tableview编号行的不明确引用

时间:2016-05-04 15:35:51

标签: swift tableview

我正在从Web服务器加载一些数据,而我对成员tableview numberOfRowsInSection的引用模糊不清?下面是我的swift文件。我在这里失踪了什么?

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 个答案:

答案 0 :(得分:0)

ViewModel继承的所有内容都没有公开tableView属性,您的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)
        // ...
}