ios - Referencing containing UIView from a subview? -


when add uiview subclass object uiview - example uiswitch inside prototype uitableviewcell , connect switch viewcontroller via ibaction, how can find out in action table row invoked uiswitch in?

i attached image clear - when uiswitch invoked, want know row invoked uiswitch in.

xcode

i think "cleanest" way have switch send action cell itself. you'll need subclass uitableviewcell implement action, , have pass along method on view controller (probably giving cell delegate view controller hooked to). fair amount of pomp; there more straight-forward less proper methods below.

one way specify tag on uiswitch when configuring table view cell in first place (i.e. in tableview:cellforrowatindexpath: method). switch.tag = indexpath.row; can retrieve row switch later asking tag.

another way search upwards through uiswitch's superviews until find uitableviewcell , find row. like:

uiview *view = switch.superview; while (view && ![view iskindofclass:[uitableviewcell class]]) { view = view.superview; } // @ point, view nil or table view cell; ask table view index path if (view) { nsindexpath *path = [tableview indexpathforcell:cell]; } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -