Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating multiple tableViews in one ViewController...this just starts out as a view controller
//
// 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) ")
}
}
@mchirico

This comment has been minimized.

Copy link
Owner Author

@mchirico mchirico commented Oct 21, 2015

Here's what it looks like, when it's working...

screenshot 2015-10-21 17 44 14

You need to add in the forCellReuseIdentifier ... here's a way to do that...

https://player.vimeo.com/external/143197982.hd.mp4?s=446c2330ceefb9675458995b34945b0f&profile_id=119

Next step is to add gestures ....

gesture35

here's a good reference: http://www.raywenderlich.com/77974/making-a-gesture-driven-to-do-list-app-like-clear-in-swift-part-1

//
//  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!


    let longPress: UILongPressGestureRecognizer = {
        let recognizer = UILongPressGestureRecognizer()
        return recognizer
        }()





    /// A simple data structure to populate the table view.
    struct PreviewDetail {
        let title: String
        let preferredHeight: Double
    }

    var 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")


        longPress.addTarget(self, action: "longPressGestureRecognized:")
        tableView.addGestureRecognizer(longPress)




    }

    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!
    }

    var sourceIndexPath: NSIndexPath? = nil
    var snapshot: UIView? = nil


    func longPressGestureRecognized(gesture: UILongPressGestureRecognizer) {
        let state: UIGestureRecognizerState = gesture.state;
        let location: CGPoint = gesture.locationInView(tableView)
        let indexPath: NSIndexPath? = tableView.indexPathForRowAtPoint(location)
        if indexPath == nil {
            return
        }

        switch (state) {

        case UIGestureRecognizerState.Began:
            sourceIndexPath = indexPath;
            let cell = tableView.cellForRowAtIndexPath(indexPath!)!
            snapshot = customSnapshotFromView(cell)

            var center = cell.center
            snapshot?.center = center
            snapshot?.alpha = 0.0
            tableView.addSubview(snapshot!)

            UIView.animateWithDuration(0.25, animations: { () -> Void in
                center.y = location.y
                self.snapshot?.center = center
                self.snapshot?.transform = CGAffineTransformMakeScale(1.05, 1.05)
                self.snapshot?.alpha = 0.98
                cell.alpha = 0.0
            })

        case UIGestureRecognizerState.Changed:
            var center: CGPoint = snapshot!.center
            center.y = location.y
            snapshot?.center = center

            // Is destination valid and is it different from source?
            if indexPath != sourceIndexPath {
                // ... update data source.

                // Here's where you move

                print("\(sourceIndexPath!.row),\(indexPath!.row)")
                let tmp = sampleData[sourceIndexPath!.row]


                sampleData[sourceIndexPath!.row] = sampleData[indexPath!.row]
                sampleData[indexPath!.row] = tmp

                // items.exchangeObjectAtIndex(indexPath!.row, withObjectAtIndex: sourceIndexPath!.row)
                // ... move the rows.
                tableView.moveRowAtIndexPath(sourceIndexPath!, toIndexPath: indexPath!)
                // ... and update source so it is in sync with UI changes.
                sourceIndexPath = indexPath;
            }

        default:
            // Clean up.
            let cell = tableView.cellForRowAtIndexPath(indexPath!)!
            cell.alpha = 0.0
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.snapshot?.center = cell.center
                self.snapshot?.transform = CGAffineTransformIdentity
                self.snapshot?.alpha = 0.0
                // Undo fade out.
                cell.alpha = 1.0

                }, completion: { (finished) in

                    self.sourceIndexPath = nil
                    self.snapshot?.removeFromSuperview()
                    self.snapshot = nil;
            })
            break
        }
    }

    // MARK: Helper

    func customSnapshotFromView(inputView: UIView) -> UIView {

        // Make an image from the input view.
        UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0)
        inputView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();

        // Create an image view.
        let snapshot = UIImageView(image: image)
        snapshot.layer.masksToBounds = false
        snapshot.layer.cornerRadius = 0.0
        snapshot.layer.shadowOffset = CGSize(width: -5.0, height: 0.0)
        snapshot.layer.shadowRadius = 5.0
        snapshot.layer.shadowOpacity = 0.4


        return snapshot
    }





}


@AudienceofOne1991

This comment has been minimized.

Copy link

@AudienceofOne1991 AudienceofOne1991 commented Mar 25, 2016

thanks man this is dope

@albincr

This comment has been minimized.

Copy link

@albincr albincr commented Mar 31, 2016

COol

@sentiasa

This comment has been minimized.

Copy link

@sentiasa sentiasa commented Jun 2, 2016

Awesome!!

@JTInfinite

This comment has been minimized.

Copy link

@JTInfinite JTInfinite commented Jun 5, 2016

I am now struggling with this but where I have custom cell in each table. I can't initialise the cell as UITableViewCell and then down cast as two different cell classes, or can i?

@abunur

This comment has been minimized.

Copy link

@abunur abunur commented Jun 22, 2016

need to beware those force unwraps...

@Yokii908

This comment has been minimized.

Copy link

@Yokii908 Yokii908 commented Jun 23, 2016

thank you!

@deya-eldeen

This comment has been minimized.

Copy link

@deya-eldeen deya-eldeen commented Jul 13, 2016

simple and effective.

@maheshgiri

This comment has been minimized.

Copy link

@maheshgiri maheshgiri commented Nov 25, 2016

thanks got idea how to do it

@Tindi

This comment has been minimized.

Copy link

@Tindi Tindi commented Dec 2, 2016

This is great! How would it work with custom cells?

@Martius108

This comment has been minimized.

Copy link

@Martius108 Martius108 commented Jan 12, 2017

what kind of identifier I can use if I like to use the 'didSelectRowAt indexPath' method for both table views?

@ciccioska

This comment has been minimized.

Copy link

@ciccioska ciccioska commented Feb 9, 2017

Good example thanks!

@luaysuarna

This comment has been minimized.

Copy link

@luaysuarna luaysuarna commented Mar 15, 2017

Nice Brother!

@atriatech94

This comment has been minimized.

Copy link

@atriatech94 atriatech94 commented Aug 15, 2017

Nice

@migueboga

This comment has been minimized.

Copy link

@migueboga migueboga commented Sep 28, 2017

Great !

@MaG21

This comment has been minimized.

Copy link

@MaG21 MaG21 commented Dec 9, 2017

One can use tags to efficiently compare one table view from the other. ie ``tableview.tag == self.tableView.tag`

@TheikChan

This comment has been minimized.

Copy link

@TheikChan TheikChan commented Jan 2, 2018

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

@solaris2013

This comment has been minimized.

Copy link

@solaris2013 solaris2013 commented Apr 13, 2018

Thanks.very helpful

@infiniteloopltd

This comment has been minimized.

Copy link

@infiniteloopltd infiniteloopltd commented Apr 23, 2018

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

@subodhkvg

This comment has been minimized.

Copy link

@subodhkvg subodhkvg commented Jul 18, 2018

Thanks for your code. It helped me a lot :)

@zunjae

This comment has been minimized.

Copy link

@zunjae zunjae commented Nov 21, 2018

Thank you. I wish the protocol implementation didn't work like this, I'd rather have one cellForRowAtIndexPath per TableView ):

@wadesaunders

This comment has been minimized.

Copy link

@wadesaunders wadesaunders commented Dec 6, 2018

@TheikChan, are you wanting to print the value of each table view, not just the first?

@LoveMeWithoutAll

This comment has been minimized.

Copy link

@LoveMeWithoutAll LoveMeWithoutAll commented Dec 2, 2019

You saved my life

@amomchilov

This comment has been minimized.

Copy link

@amomchilov amomchilov commented Jan 2, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment