Change Label of Uiswitch

get label value on uiswitch value change inside table view cell swift

declare following closure variable in UItableViewCell

var returnBlock: ((String?)-> Void)?

and in switch method just call it with labelObj.text

    @objc func switchChanged(_ sender : UISwitch!){
returnBlock?(labelObject.text)
}

In TableViewController - CellForRowAtIndexPath method where you return cell before that write following code.

    cell.returnBlock = { labelText in 
if let value = labelText {
print(value)
}
}

There is many ways to do, but i think this is proper way.

For default UITableViewCell

  @objc func switchChanged(_ sender : UISwitch!){
if let cellObj = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? UITableViewCell {
print(\(cellObj.labelObj.text ?? ""))
}
}

How can I shift my label up and down on UIswitch on and off?

It may help you. Please follow this points:

  • Add UIScrollView to your UIViewController in storyboard or XIB.
  • Initiate an NSMutableArray name it arrViews gets server response and adds view in the array.
  • Initialise UIStackViewpass arrView array in the init method.
  • After that UIStackView will be added subview of UIScrollView.
  • Add constraint programmatically to UIStackView. That's it.

    let arrViews =  createSubViews(view)
    let stackView = UIStackView(arrangedSubviews: arrViews)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.axis = .vertical
    stackView.spacing = 16
    stackView.distribution = .fill
    self.scrollView.addSubview(stackView)

    //constraints

    let leading = NSLayoutConstraint(item: stackView, attribute: .leading, relatedBy: .equal, toItem: self.scrollView, attribute: .leading, multiplier: 1.0, constant: 0)
    self.scrollView.addConstraint(leading)
    let trailing = NSLayoutConstraint(item: stackView, attribute: .trailing, relatedBy: .equal, toItem: self.scrollView, attribute: .trailing, multiplier: 1.0, constant: 0)
    self.scrollView.addConstraint(trailing)
    let top = NSLayoutConstraint(item: stackView, attribute: .top, relatedBy: .equal, toItem: self.scrollView, attribute: .top, multiplier: 1.0, constant: 0)
    self.scrollView.addConstraint(top)

    let bottom = NSLayoutConstraint(item: stackView, attribute: .bottom, relatedBy: .equal, toItem: self.scrollView, attribute: .bottom, multiplier: 1.0, constant: 0)
    self.scrollView.addConstraint(bottom)

    let equalWidth = NSLayoutConstraint(item: stackView, attribute: .width, relatedBy: .equal, toItem: self.scrollView, attribute: .width, multiplier: 1.0, constant: 0)

    self.scrollView.addConstraint(equalWidth)

    leading.isActive = true
    trailing.isActive = true
    top.isActive = true
    bottom.isActive = true
    equalWidth.isActive = true

After that when you click on the switch get a view from an array of views and hide it all other views automatically rearranged.

@objc func switchValueDidChange(_ sender: UISwitch) {
if sender == fuelSwitch{
let fuelRequiredView = arrViews[index] // get index of fuelRequiredView
if (sender.isOn == true){
// show fuel view
print("on")
fuelRequiredView?.isHidden = false
}
else{
// hide fuel view
print("off")
fuelRequiredView?.isHidden = true
}
}

Hope it will help you. Happy coding :)

UISwitch - change from on/off to yes/no

You need to implement your custom UISwitch for that. Or use one of already implemented :) (check this SO question and this post)

Changing the text on a UISwitch

As of iOS 6, you can set

@property(nonatomic, retain) UIImage *offImage;
@property(nonatomic, retain) UIImage *onImage;

Docs say:

This image represents the interior contents of the switch. The image
you specify is composited with the switch’s rounded bezel and thumb to
create the final appearance.

The size of this image must be less than or equal to 77 points wide
and 27 points tall. If you specify larger images, the edges may be
clipped.



Related Topics



Leave a reply



Submit