Search code examples
iphoneiosuitableviewpulse

When Do You Need To Reset the view.frame Property After a Transformation iOS


I'm making a horizontal table view like the Pulse news reader. I've found several examples online and have it working, but am wondering when we need to set the view.frame property after a transformation.

The examples I've found reset the frame of the horizontal table view within the vertical table view cell after the 90 degree rotation

self.tableViewCell.horizontalTableView.transform = rotateTable;
self.tableViewCell.horizontalTableView.frame = CGRectMake(0, 0, self.tableViewCell.horizontalTableView.frame.size.width, self.tableViewCell.horizontalTableView.frame.size.height);

More Context:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TableViewCell *cell = (TableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];

    CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
    self.tableViewCell.horizontalTableView.transform = rotateTable;
    self.tableViewCell.horizontalTableView.frame = CGRectMake(0, 0, self.tableViewCell.horizontalTableView.frame.size.width, self.tableViewCell.horizontalTableView.frame.size.height);

    self.tableViewCell.contentArray = [self.arrays objectAtIndex:indexPath.section];

    self.tableViewCell.horizontalTableView.allowsSelection = YES;
    cell = self.tableViewCell;
    self.tableViewCell = nil;

}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

But don't reset the frame of the horizontal table cell after the cell's transformation (rotation):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    [[NSBundle mainBundle] loadNibNamed:@"GameTableViewCell" owner:self options:nil];
    cell = self.gameTableCell;
    self.gameTableCell = nil;
}

CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
cell.transform = rotateImage;
return cell;

}

I tried resetting the cell's frame and it had no effect on the output, even if I supplied

cell.frame = CGMakeRect(200, 200, cell.frame.size.height, cell.frame.size.width)

Which should have moved the cell around the Table View, no?

If I don't reset the frame of self.tableViewCell.horizontalTableView.frame the horizontal table is rotated, but in the wrong location.

Why is it that I need to reset the frame of the horizontal Table View after rotating it, but not the individual cells (which are also rotated)?

Thanks!

// illustration by iPortable
enter image description here


Solution

  • ahh ok after reading it again I understand what you're asking ^^

    1. Setting the frame results in redrawing the view. This needs to be done because otherwise the old graphics may interfere and it looks ugly. The redraw should sharpen the draw lines too.
    2. you set your cell's frame to be positioned somewhere else (here 200,200) but it is completely normal that it has no effect. A cell cannot be elsewhere than CGPointZero. If you like to displace it, you have to create a bigger view and move the content. (the view can be translucent but it will slow down the scrolling on older devices dramatically)
    3. you ask why you don't have to redraw each cells but only the tableView. Actually I don't know this exactly but it should be in fact of the frame change of the table view. Normally a frame change occur by rotating the device so that the width changes. Now it would be pretty stupid for a developer to update each cell for the new size. Thanks to Apple the tableView calls the necessary update request for each cell which is visible on screen. Eventually the same methods are called for all views: drawRect: