[:en]Swift: Double TableViews[:]

[:en]

It’s useful to have two tableviews to compare data on the same screen. If you like this video, please subscribe and click like button on youtube.

2 tableview

 

 


import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    
        
    @IBOutlet weak var tableView1: UITableView!
    @IBOutlet weak var tableView2: UITableView!
    
    var element1: [String] = ["a","b","c","d"]
    var element2: [String] = ["x","y","z"]
    var picture1 = ["p1","p2","p3","p4"]
    var picture2 = ["p5", "p6", "p7"]
    
    @IBOutlet weak var Image: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView1.rowHeight = 44.0 // set tableView1 height

        self.tableView1.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell1")
        self.tableView2.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2")
    }
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if tableView == tableView1 {
            return self.element1.count
        }
        else {
            return self.element2.count
            
        }
        
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if tableView == tableView1 {
            
            let cell1:UITableViewCell = self.tableView1.dequeueReusableCellWithIdentifier("cell1")! as UITableViewCell
            cell1.textLabel?.text = self.element1[indexPath.row]
            cell1.imageView?.image = UIImage(named: picture1[indexPath.row] )

              // Change font size
              cell.textLabel!.font = UIFont(name:"Avenir", size:15)
             // Wrap the words
             cell.textLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
             cell.textLabel!.numberOfLines = 0
            return cell1
            
        } else {
            let cell2:UITableViewCell = self.tableView2.dequeueReusableCellWithIdentifier("cell2")! as UITableViewCell
            cell2.textLabel?.text = self.element2[indexPath.row]
            cell2.imageView?.image = UIImage(named: picture2[indexPath.row] )
            return cell2
        }
        
    }

   // Schroll to the First row
   func scrollToFirstRow() {
        let indexPath = NSIndexPath(forRow: 0, inSection: 0)
        self.tableView1.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if tableView == tableView1 {
            let indexPath = tableView.indexPathForSelectedRow!
            let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
            if let cellselected : String = currentCell.textLabel!.text{
                print("cell selected:\(cellselected)")
                 print("Index:\(indexPath.row)")
            }
        }
        else {
            let indexPath = tableView.indexPathForSelectedRow!
            let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
            if let cellselected : String = currentCell.textLabel!.text{
                print("cell selected:\(cellselected)")
                 print("Index:\(indexPath.row)")
            }
        }
    }
}



 [:]

Print Friendly, PDF & Email
Scroll to Top