Monday, 2 November 2015

Table View Using Swift

Table View Using Swift


Set up an Xcode project using the single-view application template, and make sure you opt for Swift as the language.



1) First of all Set Storyboard Like This :





2) write This Coding in Different View Controller

    Which is Given Below :


* First View Controller  :

ViewController.swift :

:- you Can Add Emojis Using (ctrl + Command + Space) -:

import UIKit

class ViewController: UIViewController {

    @IBOutlet
    var tableView: UITableView!
    
    var items: [String] = ["BMW ", "Volkswegan ", "Mercedece ","Honda City " , "Ferrari ","Hummer ","Scoda ","Lamborgini "]
    
    var image = ["bmw.jpeg","volks.jpeg","merce.jpeg","honda.jpeg","fer.jpeg","hummer.jpeg","scoda.jpeg", "lamborgini.jpeg"]

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
         return self.items.count;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell") as! myCell
        
        let stringTitle = items[indexPath.row] as String
        let strCarName = image[indexPath.row] as String
        
        cell.lbl?.text = stringTitle
        cell.img!.image=UIImage(named: strCarName)
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        
        
        let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("secondViewController") as! secondView
        
        secondViewController.data = items[indexPath.row]
        
        secondViewController.cardata = image[indexPath.row]
        
        self.navigationController!.pushViewController(secondViewController, animated: true)
        
        //self.navigationController?.presentViewController(secondViewController, animated: true, completion: nil)

    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        // self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}



myCell.swift :


import UIKit

class myCell: UITableViewCell {
    
    @IBOutlet
    var img:UIImageView!
    
    @IBOutlet
    var lbl:UILabel!
    

}


secondViewController.swift :


import UIKit

class secondView: UIViewController {

    @IBOutlet
    var lblName: UILabel!
    
    @IBOutlet var carImg: UIImageView!
    
    internal var data:String?
   
    internal var cardata = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        lblName.text = data
        carImg.imageUIImage(named:"\(cardata)")!
        
    }

    @IBAction func back(sender: AnyObject) {
        
         self.navigationController!.popViewControllerAnimated(true)

    }
    
}


No comments:

Post a Comment