Tolga Taner Tolga Taner - 3 months ago 27
Swift Question

Custom TableView

I want to pass data to another controller and then show in the custom table view.How can I connect model in SearchViewController with model in SearchViewControllerTableView

// Data comes another controller

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 */
}
}

Answer

Create a outlet of SearchViewControllerTableView in SearchViewController and assign data to SearchViewControllerTableView property model

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

 }

}

Comments