ios - Getting data out of custom UITableViewCell and back into UITableViewController -


i have uitableview comprised of custom uitableviewcells. in each cell, there uilabel , uislider. know how to, upon change in value of 1 of sliders, send new value of slider custom uitableviewcell (in separate file) uitableviewcontroller, can update array table populated?

the closest i've got far failed hack: firing setselected event when slider value changed. whilst highlights changed custom cell, event not picked didselectrowatindexpath in uitableviewcontroller.

custom uitableviewcells whilst code appreciated, conceptual/method solution looking for.

thank in advance, jamie

what need called delegate pattern.

quoting there explain mean:

delegation simple , powerful pattern in 1 object in program acts on behalf of, or in coordination with, object. delegating object keeps reference other object—the delegate—and @ appropriate time sends message it. message informs delegate of event delegating object handle or has handled. delegate may respond message updating appearance or state of or other objects in application, , in cases can return value affects how impending event handled. main value of delegation allows customize behavior of several objects in 1 central object.


these diagrams understand goes on:

architecture:

enter image description here

operation:

enter image description here

now how implement it, have do.


for objective-c:

first of all, create delegate methods of uitableviewcell. lets name contacttableviewcell.

in contacttableviewcell.h file, this:

@protocol contactcelldelegate <nsobject> @required   -(void) didmovesliderwithvalue:(float) value;  @end    @interface contacttableviewcell : uitableviewcell   @property (weak, nonatomic) id<contactcelldelegate> delegate; 

now conform tableviewcontroller delegate. let's name vc mytableviewcontroller.

in mytableviewcontroller.h, this:

@interface mytableviewcontroller : uiviewcontroller <contactcelldelegate> //use uitableviewcontroller if using instead of uiviewcontroller. 

in cellforrowatindexpath, before returning cell, add line:

cell.delegate = self; 

add implementation delegate method inside mytableviewcontroller.m.

-(void) didmovesliderwithvalue: (float) value {     nslog(@"value : %f",value);     //do whatever need value after receiving in vc } 

now let's contacttableviewcell.m. in file must have added ibaction capture value change event in slider. let's following:

- (ibaction)slidervaluechanged:(uislider *)sender {      self.mytextlabel.text = [@((int)sender.value) stringvalue]; //do whatever need in cell.   //now call delegate method send value view controller:     [delegate didmovesliderwithvalue:sender.value];  } 

when call delegate method, run implementation wrote earlier in mytableviewcontroller. whatever need in method.

what happens here cell sends message desired vc (which delegate of cell), "hey, call delegate method wrote earlier in body. sending parameters right away". vc takes parameters , whatever wanted info , @ time.


for swift:

first of all, tableviewcell.swift file, create protocol this:

@class_protocol protocol contactcelldelegate {     func didmovesliderwithvalue(value: float) } 

now in cell class, create delegate property like:

var celldelegate: contactcelldelegate? 

in slider ibaction, call delegate method this:

self.celldelegate?.didmovesliderwithvalue(slider.value) 

in vc these changes:

make conform delegate:

class mytableviewcontroller: uiviewcontroller, contactcelldelegate 

add line before returning cell in cellforrowatindexpath

cell.celldelegate = self //dont forget make conform delegate method 

add implementation of required delegate method:

func didmovesliderwithvalue(value:float) {             //do want         } 

i have kept swift part precise , summarized because should easy change detailed obj-c explanation swift implementation. if confused of pointers above, leave comment.

also see: stackoverflow answer on using delegate pattern pass data back


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -