-
-
Save mchirico/50cdb07d20b1b0f73d7c to your computer and use it in GitHub Desktop.
// | |
// ViewController.swift | |
// Delete | |
// | |
// Created by Mike Chirico on 10/21/15. | |
// Copyright © 2015 Mike Chirico. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
@IBOutlet weak var tableView: UITableView! | |
@IBOutlet weak var tableView1: UITableView! | |
/// A simple data structure to populate the table view. | |
struct PreviewDetail { | |
let title: String | |
let preferredHeight: Double | |
} | |
let sampleData = [ | |
PreviewDetail(title: "Small", preferredHeight: 160.0), | |
PreviewDetail(title: "Medium", preferredHeight: 320.0), | |
PreviewDetail(title: "Large", preferredHeight: 0.0) // 0.0 to get the default height. | |
] | |
let sampleData1 = [ | |
PreviewDetail(title: "One", preferredHeight: 160.0), | |
PreviewDetail(title: "Two", preferredHeight: 320.0), | |
PreviewDetail(title: "Three", preferredHeight: 0.0), // 0.0 to get the default height. | |
PreviewDetail(title: "More", preferredHeight: 0.0) // 0.0 to get the default height. | |
] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
tableView.dataSource = self | |
tableView.delegate = self | |
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") | |
tableView1.dataSource = self | |
tableView1.delegate = self | |
tableView1.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell1") | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
// Return the number of items in the sample data structure. | |
var count:Int? | |
if tableView == self.tableView { | |
count = sampleData.count | |
} | |
if tableView == self.tableView1 { | |
count = sampleData1.count | |
} | |
return count! | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
var cell:UITableViewCell? | |
if tableView == self.tableView { | |
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) | |
let previewDetail = sampleData[indexPath.row] | |
cell!.textLabel!.text = previewDetail.title | |
} | |
if tableView == self.tableView1 { | |
cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) | |
let previewDetail = sampleData1[indexPath.row] | |
cell!.textLabel!.text = previewDetail.title | |
} | |
return cell! | |
} | |
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { | |
print("did select: \(indexPath.row) ") | |
} | |
} | |
what kind of identifier I can use if I like to use the 'didSelectRowAt indexPath' method for both table views?
Good example thanks!
Nice Brother!
Nice
Great !
One can use tags
to efficiently compare one table view from the other. ie ``tableview.tag == self.tableView.tag`
nice example but i have issue on didSelectRowAt indexPath. I have two table view .First one is for displaying list and other is for displaying spinner in header view of first table view. When i implement didSelectRowAt indexPath it always return outer table view index path not index path of spinner table view from header view . thank you
Thanks.very helpful
Thanks for the code - was wondering how to do this.
Here's a repo with a demo project above: https://github.com/infiniteloopltd/two-tableviews
Thanks for your code. It helped me a lot :)
Thank you. I wish the protocol implementation didn't work like this, I'd rather have one cellForRowAtIndexPath
per TableView ):
@TheikChan, are you wanting to print the value of each table view, not just the first?
You saved my life
I would strongly discourage making your main tableview-owning ViewController conform to UITableViewDataSource
and UITableViewDelegate
like this, if you want to drive multiple tables.
The fact that you have branching logic in your data source methods like if tableView == self.tableView {
is an indication that you have two conjoined twin objects that are trying to get out.
Make two separate data source objects, and let them each focus on one of the two data sets, without any branching like this.
This is great! How would it work with custom cells?