nico9T
nico9T

Reputation: 2736

iOS7: UITableViewCell from one of its subviews

I have an UITableViewCell with an UIStepper on it. When the stepper value change it triggers a method:

-(void) stepperDidStep: (UIStepper*) sender

I need to get the UITableViewCell from the sender.

Until iOS7 this code worked fine:

-(void) stepperDidStep: (UIStepper*) sender
{
 UITableViewCell *cell = (UITableViewCell*) sender.superview.superview;
 //...
}

Now, in iOS7+Autolayout I get this:

UITableViewCell *cell = (UITableViewCell*) sender.superview; 

cell is UITableViewCellContentView

UITableViewCell *cell = (UITableViewCell*) sender.superview.superview;

cell is UITableViewCellScrollView (???)

Question: What is the best way to get the cell from the stepper in iOS7?

Thanks

Nicola

Upvotes: 3

Views: 454

Answers (3)

Schrodingrrr
Schrodingrrr

Reputation: 4271

Why don't you just set the tag of the UIStepper same as indexPath.Row in the data-source method cellForRowAtIndexPath?

Then in the stepperDidStep: method, get the desired cell using cellForRowAtIndexPath: like this:

-(void) stepperDidStep: (UIStepper*) sender{

     UITableViewCell *cell = (UITableViewCell*)[yourTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0];
}

Upvotes: 1

peko
peko

Reputation: 11335

Try this. I didn't test it, but i'm using similar code to find the viewcontroller of a view

- (UITableViewCell *)tableCellUnderView:(UIView *)view {
    Class class = [UITableViewCell class];
    // Traverse responder chain. Return first found UITableViewCell
    UIResponder *responder = view;
    while ((responder = [responder nextResponder]))
        if ([responder isKindOfClass:class])
            return (UITableViewCell *)responder;

    return nil;
}

Upvotes: 1

SomeGuy
SomeGuy

Reputation: 9700

Don't get the cell by checking the superview. It's too unreliable. The cell has some hidden views which complicates things even further.

Instead, subclass UIStepper and give it a custom property of UITableViewCell (possibly a weak reference) then set it to the cell when setting up the UIStepper, and then grab the cell when your stepperDidStep: method is called.

Something like:

@interface CellStepper : UIStepper
@property (nonatomic, weak) UITableViewCell* cell;
@end

.

-(void) stepperDidStep: (CellStepper*) sender
{
 UITableViewCell *cell = sender.cell;
 //...
}

Upvotes: 0

Related Questions