Search code examples
iphoneobjective-cuitableviewinterface-builderuiswitch

UISwitch propagate state


I'm still rather new to iPhone development and I tried something I didn't it was possible. I have a UIView for my TableView Section Header with a switch on it, and I also have a UITableCellView with another Switch on it. It all looks fine, but now I want to propagate the UISwitch state from the section header to all the UISwitches on the section rows. Can anyone enlighten me how can I accomplish this?

Thank you.


Solution

  • Create a mutable array and whenever you create a table cell with a switch save a reference to the switch as a new element in the array. Then when the switch in your section header is changed, loop over the array and send a message to each switch, telling it to update its state.

    UPDATE: for multiple sections, do the following

    To keep track of which switch belongs to which header, set the tag property on the switches in the headers:

    [myHeaderSwitch setTag:sectionHeader];
    

    Then have an array of mutable arrays, one for each section as described above and use this to update the switches in the correct section