HannahCarney
HannahCarney

Reputation: 3631

Find frame of UIView of UITableViewCell after UITableViewScrollPosition

So I do this to program a scroll in my table view controller to my first cell.

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
                [self.tableView scrollToRowAtIndexPath:indexPath
                                     atScrollPosition:UITableViewScrollPositionBottom
                                             animated:YES];

Then I want to fetch the new frame of this view:

 NSArray *a=[self.tableView visibleCells];
UIView *view = [a firstObject];

However, the frame that this view gives me (which is the first one in my array of cells) returns the position before the scroll occurred.

I have tried putting it in an animation block to make sure it has finished scrolling before it tries to find it's position

Upvotes: 0

Views: 157

Answers (2)

HannahCarney
HannahCarney

Reputation: 3631

So it turns out it is because I had it wrapped in an animation within an animation and so the SECOND animation wasn't completing before it checked for the frame:

 [UIView animateWithDuration:0.5 animations:^{

                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
                [self.tableView scrollToRowAtIndexPath:indexPath
                                     atScrollPosition:UITableViewScrollPositionBottom
                                             animated:NO];

            }completion:^(BOOL finished)
            {
                 NSArray *a=[self.tableView visibleCells];
                InstructionView *iv = [[InstructionView alloc] initWithTargetView:[a firstObject] andInstructionType:kNeedYouInstruction];
                iv.delegate = self;
                [iv show];
            }];

changing the second animation to NO stopped my problem :D

Upvotes: 0

Hermann Klecker
Hermann Klecker

Reputation: 14068

The frame is always related to the super view. If you are interested then debug (or print out) what the super view of the cell view is. It is not directly the table view. (Used to be until iOS 7 or 8 or so but now it is embedded in some view object that scrolls along with the cell.)

You could try fetching the frame of its super view. However, you'd be saver comparing the origin of the cell with the base view (self.view from the UIViewController's point of view which is the UITableView in case of a standard UITableViewController). For doing so you can use the convertPoint:fromView or convertPoint:toView methods.

Upvotes: 1

Related Questions