I recorded code examples. Check this.
If you have questions, ask me whenever you want.
MainViewController
class MainViewController: UIViewController, ListViewControllerProtocol {
@IBOutlet weak var button:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func toList() {
var listViewController = ListViewController(nibName: "ListViewController", bundle: nil)
listViewController.delegate = self
self.pushViewController(listViewController, animated: true)
}
@IBAction func buttonClicked(sender: UIButton) {
self.toList()
}
func onSelectObj(obj:NSDictionary) {
}
}
Listviewcontroller
class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView:UITableView?
var delegate:ListViewControllerProtocol?
var identifier = "ListViewController"
var data = []
override func viewDidLoad() {
super.viewDidLoad()
self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: identifier)
self.updateData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 37
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! UITableViewCell
var row = UInt(indexPath.row)
var obj = self.data[0] as! NSDictionary
cell.textLabel?.text = obj["name"]
return cell;
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
var obj = self.data[0] as! NSDictionary
self.selectObj(obj)
}
private func selectObj(obj: NSDictionary) {
self.delegate?.onSelectObj(obj)
self.navigationController?.popViewControllerAnimated(true)
}
private func updateData() {
self.data = someDeserializedData
self.tableView!.reloadData()
}
}
protocol ListViewControllerProtocol {
func onSelectObj(obj:NSDictionary)
}
source
share