Jamie February 2016

Getting data out of custom UITableViewCell and back into UITableViewController

I have a UITableView comprised of custom UITableViewCells. In each cell, there is a UILabel and a UISlider. Does anyone know how to, upon a change in value of one of the sliders, send the new value of the slider from the custom UITableViewCell (in a separate file) to the UITableViewController, so that I can then update the array from which the table was populated?

The closest I've got so far is a failed hack: firing a setSelected event when a slider value is changed. Whilst this highlights the changed custom cell, the event is not picked up by didSelectRowAtIndexPath in the UITableViewController.

Custom UITableViewCells Whilst code is always appreciated, a conceptual/method solution is what I am looking for.

Thank you in advance, Jamie

Answers


NSNoob February 2016

What you need is called Delegate Pattern.

Quoting from there to explain what does it mean:

Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.


These diagrams will help you understand what goes on:

Architecture:

enter image description here

Operation:

enter image description here

Now as to how to implement it, this is what you have to do.


For Objective-C:

First of all, create delegate methods of your UITableViewCell. Lets name it ContactTableViewCell.

In your ContactTableViewCell.h file, do this:

@protocol ContactCellDelegate <NSObject>
@required


-(void) didMoveSliderWithValue:(float) value;

@end



@interface ContactTableViewCell : UITableViewCell


@property (weak, nonatomic) id<ContactC 

Post Status

Asked in February 2016
Viewed 1,094 times
Voted 7
Answered 1 times

Search




Leave an answer