How to get each cell's position

| | August 7, 2015

I need to know where exactly each cell in a tableView is at while scrolling.

Tried this but it just returns null.. probably does not make sense.

self.cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (self.cell == nil) {
    self.cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    NSNumber *cellOriginY = [NSNumber numberWithFloat:self.cell.frame.origin.y];

    [self.cellPositionArray addObject:cellOriginY];

I first tried to manage this function I’m trying to add with indexPath like this :

CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.view.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.tableView indexPathForRowAtPoint:visiblePoint];

Which kind of works but then later I found out multiple cells with same indexPath get selected. I guess it’s because indexPath is not unique so that leaves me with no other choices than managing it with each cell’s Y axis.
Any other idea..?

3 Responses to “How to get each cell's position”

  1. Matthew Lawrence Bailey on November 30, -0001 @ 12:00 AM

    When I want to try and see how my cells are formatting themselves I’ll often call in a label to put on my cell that displays the information I need from it. I’m assuming you are using a custom UITableView class and that is why you call self.cell instead of creating and returning a cell as you would do in the required UITableViewDataSourceProtocol‘s. You don’t have your functions header so I can’t really tell, but use this protocol and make sure you have self.delegate = self. That should display each cell with the info you are looking for. If you don’t have a grouped tableView then section would just display 0 everytime. Each IndexPath has two values, section and row.

     -(UITableViewCell *)cellForRowAtIndexPath:(UITableView *)tableView forIndexPath:(NSIndexPath *)indexPath {
            if(self.cell == nil) {
                   UILabel *label = [UILabel new];
                   label.text = [NSString stringWithFormat:@"Cell %ld in section %ld starting at point %f", indexPath.row, indexPath.section, self.cell.fram.origin.y ];
                   [self.cell addSubView:label]; //Forgot to add this to first post.
                   //New Code For CustomViews
                   CustomView *customView = [self.customViews objectAtIndex:indexPath.row];
                   [self.cell addSubView:customView];

    Hope you can use this to find what you are looking for. Try adding more info about the class you are doing this in such as header file if you have more questions.
    //Answer to your other question:
    Best way I can think of to work through a simple tableView is using an NSArray. Simplest way is to add an array property to the tableView class.

    @property (strong, nonatomic, readwrite) NSMutableArray *customViews;

    then in your ‘ViewController.m’ in ‘ViewDidLoad’ initialize your array and items in it after initializing the custom table view class.

    'customTableView.customImages = [NSArray arrayWithObjects: cutomView1, customView2, etc..., nil];'

    After you have that its simple I’ll add code in my previous answer to how u set the views to the cell. It will make it so row 0 will have the objectAtIndex[0], row 1 objectAtIndex[1] etc.

    Make sure to update your numberOfRowsInSection protocol method with
    `return [self.customImages count]’
    so that your table view doesn’t create more cells than it needs.

  2. Figured out with the code I already had.
    It’s probably not a good way to do this but.. for now!

    CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.view.bounds.size};
    CGPoint zeroPoint = CGPointMake(CGRectGetMinX(visibleRect), CGRectGetMinY(visibleRect));

    And each time you scroll, check –

    if (zeroPoint.y == (int)visibleIndexPath.row * 380)

    380 is the height of each cell.
    Although, it only works when you scroll slowly so you’d have to enhance the if statement.

  3. Try this, if cell.contentView doesn’t work, might be use any control on cell.contentView.

    CGPoint point = [cell.contentView.superview convertPoint:cell.contentView.frame.origin toView:nil];

Leave a Reply