Tuesday, 8 December 2015

SWIFT: DropDown using custom button and uitableview


class customViewController: ViewController, UITableViewDataSource, UITableViewDelegate{

    var dropDownTableView = UITableView()
    var dropDownBooleanValue = 1
    let rowHeight : CGFloat = 40
    var selectedcityButton = UIButton()
   var cityArray = ["Noida", "Delhi", "Punjab", "Jaipur", "Nagaland", "New Delhi", "Mumbai", "Bihar"]

   override func viewDidLoad() {
/*--------------------------------------- TableView -------------------------------------------*/

     dropDownTableView.delegate =   self
        dropDownTableView.dataSource =   self
        dropDownTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "DropDownCell")
        dropDownTableView.frame = CGRectMake(20, 0, mainParentScrollView.frame.size.width/2 - 40, 0)
        dropDownTableView.separatorStyle = .None
        dropDownTableView.backgroundColor = UIColor.whiteColor()


/*-----------------------------------------custom Button -------------------------------------------*/

let cityDropDownButton = UIButton()
        cityDropDownButton.frame = CGRectMake(15, 30, self.view.frame.width - 30, 38)
        cityDropDownButton.setTitle("Selecity City", forState: .Normal)
        cityDropDownButton.backgroundColor = UIColor.whiteColor()
        cityDropDownButton.addTarget(self, action: Selector("cityDropDownButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
        cityDropDownButton.setTitleColor(UIColor.greenColor(), forState: .Normal)
        cityDropDownButton.layer.borderWidth = 1
        cityDropDownButton.layer.borderColor = UIColor(netHex:0x0c8506).CGColor
        cityDropDownButton.layer.cornerRadius = 5
        cityDropDownButton.layer.masksToBounds = true
        let Imgright = UIImage(named: "greendown.png")
        cityDropDownButton.setImage(Imgright, forState: UIControlState.Normal)
        cityDropDownButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
        cityDropDownButton.imageEdgeInsets = UIEdgeInsetsMake(0, cityDropDownButton.frame.size.width - (Imgright!.size.width + 10) , 0, 0)
        cityDropDownButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, (Imgright!.size.width)+15)
        self.view.addSubview(cityDropDownButton)
  }

    func cityDropDownButtonAction(button:UIButton){
        var ypos:CGFloat = 0
        var xpos:CGFloat = 0
        self.selectedcityButton = button
        UIView.animateWithDuration(0.2, animations: {
            button.imageView!.transform = CGAffineTransformRotate(button.imageView!.transform, 180 * CGFloat(M_PI/180))
            }, completion: { _ in
        })
        ypos = button.frame.origin.y + button.frame.height
        xpos = button.frame.origin.x
        
        if(dropDownBooleanValue == 0){
            UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.TransitionNone, animations: {
                self.dropDownTableView.frame.size.height = 0
                self.alertView.frame.size.height -= 150
                }, completion: { _ in
                    self.dropDownTableView.hidden = true
            })
            
            for allSubViews in self.view.subviews{
                allSubViews.userInteractionEnabled = true
            }
            dropDownBooleanValue = 1
        }
        else{
            dropDownTableView.frame.origin.x =  xpos 
            dropDownTableView.frame.size.width = button.frame.width 
            dropDownTableView.frame.origin.y = ypos
            
            self.view.addSubview(dropDownTableView)
            
            UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.TransitionNone, animations: {
                self.dropDownTableView.frame.size.height = 200
                }, completion: { _ in
                    self.dropDownTableView.hidden = false
            })
            
            dropDownTableView.reloadData()
            dropDownBooleanValue = 0
        }
    }

/*--------------------------------------- Delegate methods TableView -------------------------------------------*/

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.cityArray.count
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("DropDownCell", forIndexPath: indexPath) as UITableViewCell
        
        cell.backgroundColor = UIColor.whiteColor()
        cell.textLabel?.textColor = UIColor.blackColor()
        cell.textLabel?.textAlignment = .Center
        
        cell.textLabel?.text = self.cityArray[indexPath.row]
        if((self.cityArray.count - 1) != indexPath.row){
            cell.layer.addBorder(UIRectEdge.Bottom, color: UIColor(netHex:0x0c8506), thickness: 1)
        }
        return cell
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return self.rowHeight
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        self.selectedcityButton.setTitle(self.cityArray[indexPath.row], forState: .Normal)
        dropDownGesturebuttonAction()
    }
    
    func dropDownGesturebuttonAction(){
        dropDownBooleanValue = 0
        self.cityDropDownButtonAction(self.selectedcityButton)
    }

}

No comments:

Setting Up Multiple App Targets in Xcode from a Single Codebase

 To create two different apps (like "Light" and "Regular") from the same codebase in Xcode, you can follow these steps b...