ios - Segmented control in tableCell -


i have problem segmented control in table cell. no action of segmented control

segmentedfilter.addtarget(self, action: #selector(productlistfiltercell.filterselected), forcontrolevents: .allevents) 

target function:

func filterselected() {     if let order = productlistviewmodel.orderby(rawvalue: segmentedfilter.selectedsegmentindex), let change = orderchanged{         change(order)     } } 

it possible have segmented control in table cell ?

it incorrect call addtarget in prepareforreuse, because method called when cell no longer needed. reused cells have selector on them.

the second problem target: passing self, data source table, selector refers cell. if bring reused cell scrolling , down several times, control trigger event, , app crash unknown selector message.

you can fix moving method data source class, adding target in cellforrowatindexpath, , removing target in prepareforreuse.

segmentedfilter.addtarget(self, action: #selector(productlistdatasource.filterselected), forcontrolevents: .allevents) //                                               ^^^^^^^^^^^^^^^^^^^^^^ 

Comments