hannes
hannes

Reputation: 537

Change Data with Switch on UITableViewCell

i am quite new to iOS and Swift and want to solve a Problem with UITableViewCell

I have a ControllerClass with a UITableView that has a Custom UITableViewCell called ArtistCell as following

public class ArtistCell: UITableViewCell {

var value : Bool = false

@IBOutlet weak var artistSwitch: UISwitch!
@IBOutlet weak var artistTextField: UITextField!

@IBAction func changedBoolValue(sender: UISwitch) {
    self.value = sender.on
}

public func configure(text: String, enabledArtist: Bool) -> Bool{
    self.artistSwitch.on = enabledArtist
    self.value = enabledArtist
    self.artistTextField.text = text

    return self.value

}

In this class as you can see, there is a textfield and a switch. If this switch is clicked the value of this list item in my ViewController should be changed

import UIKit

class ProfileViewController: UIViewController, WCSessionDelegate, UITableViewDataSource, UITableViewDelegate {


...

@IBOutlet weak var tableView: UITableView!
var profile : UserProfile = UserProfile()



override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 10
  ...  
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.profile.artists.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //get my own cell  
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ArtistCell

    //initialize cell and get back the actual Value of the switch and set it to my object???
    let boolValue : Bool = cell.configure(     self.profile.artists[indexPath.row].name, enabledArtist: self.profile.artists[indexPath.row].display.boolValue )

    //this following value should be set in my object
    profile.artists[indexPath.row].display = boolValue

    return cell

}
}

now i want to know how i should set the bool value of my switch to my object?

Upvotes: 0

Views: 1731

Answers (2)

zuziaka
zuziaka

Reputation: 573

Define protocol before ArtistCell:

protocol ArtistTableViewCellDelegate {
    func didChangeSwitchValue(value: Bool, artistName: String)
}


public class ArtistCell: UITableViewCell {

    var delegate: ArtistTableViewCellDelegate?
    var value: Bool =  false
    @IBOutlet weak var artistSwitch: UISwitch!
    @IBOutlet weak var artistTextField: UITextField!

    @IBAction func changedBoolValue(sender: UISwitch) {
        self.value = sender.on
        delegate?.didChangeSwitchValue(value, artistName: artistTextField.text!)
    }
}

And in your ViewController:

class ProfileViewController: UIViewController, WCSessionDelegate, UITableViewDataSource, UITableViewDelegate, ArtistTableViewCellDelegate {

    //...
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //get my own cell
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ArtistCell

        //initialize cell and get back the actual Value of the switch and set it to my object???
        let boolValue : Bool = cell.configure(     self.profile.artists[indexPath.row].name, enabledArtist: self.profile.artists[indexPath.row].display.boolValue )

        //this following value should be set in my object
        profile.artists[indexPath.row].display = boolValue
        cell.delegate = self
        return cell

    }

    //...

    func didChangeSwitchValue(value: Bool, artistName: String) {
        //do sth with your value
    }

}

You can also do some refactor in ArtistCell to achieve:

func didChangeSwitchValue(value: Bool, artistID: Int)

or:

func didChangeSwitchValue(value: Bool, artist: YOUR_ARTIST_TYPE)

Upvotes: 2

Alexey Pichukov
Alexey Pichukov

Reputation: 3405

Now you have default value from switch when your cell is created. To set new value for dataModel in ViewController when your switch state changed you can use delegate mechanism.

Create protocol for your action:

protocol SwitchChangedDelegate {
    func changeStateTo(isOn: Bool, row: Int)
}

Make your ProfileViewController class confirm this protocol:

class ProfileViewController: UIViewController, WCSessionDelegate, UITableViewDataSource, UITableViewDelegate, SwitchChangedDelegate {
    ...
    func changeStateTo(isOn: Bool, row: Int) {
        // here update your dataModel
        profile.artists[row].display = isOn
    }
    ...
}

Add to your ArtistCell delegate object with type on protocol and row variable:

var delegate: SwitchChangedDelegate?
var row: Int?

Set delegate and row at your func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath):

cell.delegate = self
cell.row = indexPath.row

Call protocol method in your changedBoolValue func:

@IBAction func changedBoolValue(sender: UISwitch) {
    ...
    self.delegate?.changeStateTo(sender.on, row: row)
}

Upvotes: 1

Related Questions