swift - Set object properties using a UISwitch in a custom cell -
i'm trying should simple, i'm having issues due inexperience swift.
i have viewcontroller
has tableview
inside of custom cells populated array of objects (called alllistitems
). these objects created using realm model object
, i'm using instead of core data, think might pertinent. each custom cell has uiswitch
in it, , ideally i'd set when user toggles uiswitch
, modifies boolean isselected
property indexpath.row
, , appends object separate array, called selectedlistitems
.
all of searching through so, tuts+, , appcoda has revealed should using protocol - delegate pattern here, protocol in custom cell class , delegate in viewcontroller
class. after flailing away @ of day haven't had luck, however, think might due arrays being realm model objects.
as mentioned, i'm new swift , programming in general, eli5 responses appreciated! in advance!
for reference, here custom cell:
import uikit class alllistitemstableviewcell: uitableviewcell { @iboutlet var toggleisselected: uiswitch! @iboutlet var listitemlabel: uilabel! override func awakefromnib() { super.awakefromnib() // initialization code } override func setselected(selected: bool, animated: bool) { super.setselected(selected, animated: animated) // configure view selected state } }
instead of suggested protocol / delegate pattern use callback.
easy in swift.
in table view cell declare optional variable closure
var callback : ((uitableviewcell, bool) -> void)?
and call in ibaction
switch
@ibaction func switchchanged(sender : uiswitch) { callback?(self, sender.on) }
in cellforrowatindexpath
set callback
cell.callback = { (tableviewcell, switchstate) in if let indexpath = self.tableview.indexpathforcell(tableviewcell) { // index path , switch state } }
to pass cell can useful if cell moved meanwhile recent index path.
Comments
Post a Comment