3

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

4

3 回答 3

1

你为什么不直接设置与方法中tagUIStepper一样?indexPath.Rowdata-sourcecellForRowAtIndexPath

然后在该stepperDidStep:方法中,使用如下方式获取所需的单元格cellForRowAtIndexPath:

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

     UITableViewCell *cell = (UITableViewCell*)[yourTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0];
}
于 2013-10-02T15:09:16.090 回答
1

尝试这个。我没有测试它,但我正在使用类似的代码来查找视图的视图控制器

- (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;
}
于 2013-10-02T15:07:23.633 回答
0

不要通过检查超级视图来获取单元格。太不靠谱了。单元格有一些隐藏的视图,这使事情变得更加复杂。

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;
 //...
}
于 2013-10-02T14:56:33.293 回答