Monday 18 September 2017

UITableView With Checkbox





import UIKit

class GroupsListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
      
    let groupListNames = ["Group-1", "Group-2", "Group-3", "Group-4", "Group-5", "Group-6"]
 /*   let groupListNames = ["Group-1", "Group-2", "Group-3", "Group-4", "Group-5", "Group-6", "Group-7", "Group-8","Group-9", "Group-10", "Group-11", "Group-12"]*/
     var count : UInt = 0

    
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        
        return groupListNames.count
    }
   
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   
         if(tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark){
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none

            img2 = UIImageView()
            img2.sizeToFit()
            img2.frame = CGRect(x: 10, y: 10, width: 24, height: 24)
            img2.image = UIImage(named: "UnSelectedIcon.png")
            img2.contentMode = UIViewContentMode.scaleAspectFit
            tableView.cellForRow(at: indexPath)?.accessoryView = img2
            tableView.cellForRow(at: indexPath)?.backgroundColor = UIColor.clear

         }else{
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
            img1 = UIImageView()
            img1.sizeToFit()
            img1.frame = CGRect(x: 10, y: 10, width: 24, height: 24)
            img1.image = UIImage(named: "SelectedIcon.png")
            img1.contentMode = UIViewContentMode.scaleAspectFit
            tableView.cellForRow(at: indexPath)?.accessoryView = img1
            tableView.cellForRow(at: indexPath)?.backgroundColor = UIColor.clear
   
         }
    }

    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! GroupListsTableViewCell
        cell.GroupNameLabel.text = groupListNames[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if count < 11 {
        img2 = UIImageView()
        img2.sizeToFit()
        img2.frame = CGRect(x: 10, y: 10, width: 24, height: 24)
        img2.image = UIImage(named: "UnSelectedIcon.png")
        img2.contentMode = UIViewContentMode.scaleAspectFit
        cell.accessoryView = img2
        cell.backgroundColor = UIColor.clear
           count  = count + 1
        }
    }

    
   
    
    var img1 :UIImageView!
    var img2 :UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }
    
  /*  @IBAction func funcGotoMyProfileVC(_ sender: Any) {
        self.view.endEditing(true)
        let signUpVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
        self.navigationController?.pushViewController(signUpVC!, animated: false)
    }*/
    
    @IBAction func funcGotoLoginVC(_ sender: Any) {
        self.view.endEditing(true)
        let signUpVC = self.storyboard?.instantiateViewController(withIdentifier: "AddJobViewController")
        self.navigationController?.pushViewController(signUpVC!, animated: false)
    }

}

NaOpen another controller







import UIKit

class ViewController : UIViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
   
        
    }
    
  
    
    @IBAction func funcGotoGroupListVC(_ sender: Any) {
        self.view.endEditing(true)
        let signUpVC = self.storyboard?.instantiateViewController(withIdentifier: "GroupsListViewController")
        self.navigationController?.pushViewController(signUpVC!, animated: false)

    }
}

Hide Keyboard




import UIKit

class ViewController : UIViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action:  #selector(self.dismissKeyboard))
        view.addGestureRecognizer(tap)
        
    }
    
    func dismissKeyboard() {
        view.endEditing(true)
    }
}