How to focus on a grouped UITableViewCell on an iPad?

I am converting an iPhone application to an iPad and I am stuck in an innocent and seemingly trivial positioning problem. I have a UITableViewCell that only contains a UISwitch centered in the cell. The cell is in a UITableView with a Grouped style.

On iPhone, I just set the center of the switch property to the center of the cell in tableView:cellForRowAtIndexPath: This is enough for the iPhone, because the center of the cell is the same, regardless of the style of the table. This also happens on the iPhone, but the offset is smaller, so the difference is more subtle. Here is the code for the iPhone:

  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; UISwitch *view = [[UISwitch alloc] initWithFrame:CGRectZero]; [view addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; view.center = cell.center; cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell.contentView addSubview:view]; [view release]; 

On the iPad, without changing any code, the switch position is still in the center of the iPhone screen. This is bad, since the iPad is much larger than the iPhone. So I moved the positioning code to tableView:willDisplayCell:forRowAtIndexPath: and now on the other side of the center (closer to the right side of the screen).

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { UISwitch *s = (UISwitch *)[cell.contentView viewWithTag:SwitchTag]; s.center = CGPointMake(cell.contentView.center.x,s.center.y); //pushes the switch too far to the right 

}

And yet, when I register the various coordinates and frames that interest me, I get what I expect: the x coordinate of the center of the cell is 384.0, which is half the width of the screen. I also tried recounting the entire UISwitch frame, but since I am using screen values, the switch ends in the same place, too far to the right.

I think some conversion happens after tableView:willDisplayCell:forRowAtIndexPath: is called (or before, even) to make all these numbers pointless. It seems that the whole cell is shifted by x, where x is the distance from the edge of the screen to the beginning of the cell, even if it is not specified in the frame property.

I am sure there is something trivial that I am missing. Please enlighten me.

+4
source share
2 answers

This is probably not the most elegant solution and, of course, does not begin to penetrate into the basic behavior, why there is no place where you expect them to mess with TableCell views, BUT

 view.center =CGPointMake(tableView.center.x-(view.frame.size.width/2), cell.center.y) ; 

if you use this row instead of the one you posted, it should happily (at least look like this in my quick tests) put this switch in the middle of the table.

0
source

Grouped cells work a little differently. I had a similar problem, but you accept things about the center. Sometimes temporarily setting the background color for all types of different types (cell.backgroundColor, cell.contentView.backgroundColor, etc.), you can visualize it a bit.

You may have to do something like this:

 s.center = CGPointMake(s.superview.bounds.size.width / 2.0,s.center.y); 

If this does not work, I believe that you are right, and does something with the coordinates when it does. In this case, you will have to write your own subclass of UITableViewCell, which is not the worst in the world.

0
source

All Articles