自定义TableView

时间:2016-08-10 11:35:18

标签: swift uitableview

我想将数据传递给另一个控制器然后显示在自定义表视图中。如何将SearchViewController中的模型与SearchViewControllerTableView中的模型连接

//数据来自另一个控制器

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let navController = segue.destinationViewController as! UINavigationController

    if  let controller : SearchViewController = navController.topViewController as? SearchViewController {

        if  segue.identifier == "segue"{

            controller.model = movies    // pass data to Another Controller

        }
    }
}

class SearchViewController: UIViewController{


    var model = [Movie] ()

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

class SearchViewControllerTableView: UITableView,UITableViewDataSource,UITableViewDelegate {

    var model = [Movie]()

    override func awakeFromNib() {
        self.delegate = self
        self.dataSource = self
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1  // should be model.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let myCell = self.dequeueReusableCellWithIdentifier("mycell", forIndexPath: indexPath) as! CustomCell

        /* Should be  myCell.movieTitle.text = model[indexPath.row].title

         urlToImage((string: model[indexPath.row].thumbnail!)) { (image) in

         myCell.movieImage.image = image

         }
         return myCell  */
    }
}

1 个答案:

答案 0 :(得分:0)

在SearchViewController中创建SearchViewControllerTableView的插座并将数据分配给SearchViewControllerTableView属性模型

class SearchViewController: UIViewController {

   @IBOutlet weak var tableView: SearchViewControllerTableView!
    var model = [Movie] ()

 override func viewDidLoad() {
    super.viewDidLoad()

  // assign data to SearchViewControllerTableView model
      tableView.model = self.model

 }

}