James Franco
James Franco

Reputation: 4716

Obtaining the x / y frame coordinates of where a control ends

I am currently returning a UITableViewCell* which looks like this

Image --- Text ---  Button --- Image

The code that I am using looks like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"SimpleTableItem";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];



    if (cell == nil) {


        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 

    }


    cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row];            //Add cell text

    cell.imageView.image = [UIImage imageNamed:@"Test.jpeg"];                      //Add cell image

    float f = [cell.textLabel alignmentRectInsets].right;



    UIButton *scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  //ADD UIButton

    scanQRCodeButton.frame = CGRectMake(?, ?, 50.0f, 50.0f);                     //Coordinates for image view

    [scanQRCodeButton setTitle:@"Hello" forState:UIControlStateNormal];

    [cell.contentView addSubview:scanQRCodeButton];



    UIImageView *preArrowImage =[[UIImageView alloc]init ];                     //ADD Image View

    preArrowImage.image =[UIImage imageNamed:@"Test.jpeg"];

    preArrowImage.frame = CGRectMake(?, ?, 10, 30);                             //Coordinates for image view

    [cell.contentView addSubview:preArrowImage];



    return cell;
}

As mentioned before this is what the TableViewCell would look like

 ImageA --- Text ---  Button --- ImageB

I am aligning my button in the TableViewCell using the following

 scanQRCodeButton.frame = CGRectMake(?, ?, 50.0f, 50.0f);

and I am aligning the UIImageView using the following

 preArrowImage.frame = CGRectMake(?, ?, 10, 30);  

Now my question is that in reality I am using hard coded variables instead of ? . How can I find out the x frame coordinate of where the Text ends so I could utilize that value instead of hard-coding.

Upvotes: 0

Views: 41

Answers (1)

Dima
Dima

Reputation: 23634

First of all there are some issues with your code. You are adding subviews to the cell's contentView every time the cell is reused. That code should instead be in the if (cell == nil) body so that it only happens once. You will of course then need a reference to those subviews which you could do by either using a UITableViewCell subclass with dedicated variables or by setting tags.

Next up, generally speaking you can do something like:

CGFloat labelRightEdge = CGRectGetMaxX(textLabel.frame);

I'm not sure it will work in this case and cause you problems when you are trying to align things with UITableViewCell built in views. Those views are laid out internally and aren't really meant to be used in conjunction with custom subviews. I would instead recommend building all of the views yourself by making a UITableViewCell subclass and then you can lay out all the views as you see fit in layoutSubviews.

Upvotes: 3

Related Questions