xyz code xyz code - 4 months ago 28
Swift Question

How can i hide a Section when UISwitch is Changed

Here is my

tableView
and want to hide the section "eventdays"
how can i do that ?

Hope you can help me.

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// set data within item list
self.going = ["You can go?","Whos going"]
self.eventdays = ["Monday", "Sunday", "Wednesday"]
self.others = ["Bread", "Butter", "Paneer"]

// set table view delegate and data source
self.myTableView.delegate = self
self.myTableView.dataSource = self
}

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



// Mark: - Table view data source and delegate

// set number of sections within table view
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}

// set number for rows for each setion
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return self.going.count
}
if section == 1 {
return self.eventdays.count
}
if section == 2 {
return self.others.count
}

return 0
}

// set header title for each section
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Can Go?"
}
if section == 1 {
return "Days"
}
if section == 2 {
return "Others"
}

return "Default Title"
}

// set cell content for each row
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

// deque reusable cell
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row



//Configure the switches for swipe options
let teilnehmenSwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
teilnehmenSwitch.on = false

let eventDaySwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
eventDaySwitch.on = false




switch(indexPath.section){
case 0:
cell.textLabel?.text = going[row]
if row == 0 {

cell.accessoryView = teilnehmenSwitch

}

case 1:
cell.textLabel?.text = eventdays[row]
cell.accessoryView = eventDaySwitch
case 2:
cell.textLabel?.text = others[row]
default:
cell.textLabel?.text="Others"
}

// return cell
return cell
}


And how can i find out what switch is changed so i can save the data in a
DataBase
.

Thanks in advance

Answer

Try to change your code this way, Add one action with your UISwitch ValueChanged event and reload the tableView.

@IBAction func switchValueChange(sender: UISwitch) {
    tableView.reloadData()
}    

Now change your UITableViewDataSource method like this way

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if mySwitch.on {
        return 3
    }
    else {
        return 2
    }
}

// set number for rows for each setion
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return self.going.count
    }
    if section == 1 {
        if mySwitch.on {
            return self.eventdays.count
        }
        else {
            return self.others.count
        }
    }
    if section == 2 {
        return self.others.count
    }

    return 0
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "Can Go?"
    }
    if section == 1 {
        if mySwitch.on {
            return "Days"
        }
        else {
            return "Others"
        }
    }
    if section == 2 {
        return "Others"
    }
    return "Default Title"
}