简体   繁体   中英

Change position of label in a custom UITableViewCell connected with xib file when orientation change

i have a custom UITableviewCell xib, connected with my uitableview, and i'll do it in this way:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];

    cell = myCell;
    self.myCell = nil;
}

[self configureCell:cell atIndexPath:indexPath];
return cell;
}

then when change the orientation of the device, i want change the position of some element in my custom UITableViewCell so in this method:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {

    CGRect myframe = CGRectMake(600, self.arrowLabel.frame.origin.y, self.arrowLabel.frame.size.width, self.arrowLabel.frame.size.height);
        self.arrowLabel.frame = myframe;
} else {

    CGRect myframe = CGRectMake(400, self.arrowLabel.frame.origin.y, self.arrowLabel.frame.size.width, self.arrowLabel.frame.size.height);
        self.arrowLabel.frame = myframe;
}
}

i change the frame.origin.y and frame.origin.y of my arrowLabel connected with my UITableViewCell xib but change only the position of the last row of the tableview, the other label in the other row remain in the same position, so my question is how i can reload the table view?...i have also tried with [self.tableview reloadData];...but don't work...any idea?

You may want to try putting the frame change codes in your cellForRowAtIndexPath . Basically in that method you have something like:

if (portrait) {
    // use portrait frames
} else {
    // use landscape frames
}

And then in your willRotate method you can call [tableView reloadData] .

Edit:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

for (UITableViewCell *cell in [tableView visibleCells]) {

    if (portrait) {
        CGRect myframe = CGRectMake(600, cell.arrowLabel.frame.origin.y, cell.arrowLabel.frame.size.width, cell.arrowLabel.frame.size.height);
        cell.arrowLabel.frame = myframe;
    } else {
        CGRect myframe = CGRectMake(400, cell.arrowLabel.frame.origin.y, cell.arrowLabel.frame.size.width, cell.arrowLabel.frame.size.height);
        cell.arrowLabel.frame = myframe;
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM