UITableViewCell图像旋转后不调整大小

时间:2014-07-29 16:25:16

标签: ios uitableview

屏幕旋转后,我无法在UITableViewCell中调整图像大小。在我的cellForRowAtIndexPath中,我检测到设备的宽度,如果宽度小于480,我将图像设置为屏幕宽度,然后将高度设置为(图像高度/图像宽度*屏幕宽度)到保持图像缩放。如果设备的宽度大于480我会做同样的事情,除了我设置宽度等于屏幕的一半,使缩放的图像稍微小一些。

下面是我描绘了页面如何以纵向和横向布局呈现的图像。当我从横向旋转到纵向时,可见图像仍保留在屏幕上,除非我滚动到离开屏幕的位置,否则不会重新调整大小。

enter image description here

同样的事情在我从画像到风景时起作用,它在右侧留下一个大的开放空间,直到我向下滚动,然后它加载得很好。我觉得这很简单,例如添加autoresizingmask,但我无法弄清楚它的位置以及如何解决它:

[self.myTableView setAutoresizesSubviews:YES];
[self.myTableView setAutoresizingMask:
    UIViewAutoresizingFlexibleWidth |
    UIViewAutoresizingFlexibleHeight];

以下是我的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIndentifier = @"BasicCell";
    UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];

    CGRect result = [UIScreen mainScreen].bounds;
    CGFloat width = CGRectGetWidth(result);
    CGFloat height = CGRectGetHeight(result);

    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        result.size = CGSizeMake(width, height);
    } else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
        result.size = CGSizeMake(height, width);
    }

    if(result.size.width <= 480) {
        Companies *item = _feedItems[indexPath.row];
        myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.listTableView.bounds), (item.imageName.size.height / item.imageName.size.width) * CGRectGetWidth(self.listTableView.bounds))];
        myImageView.tag = indexPath.row;
        myImageView.image = item.imageName;

        [myCell addSubview:myImageView];
    }

    if(result.size.width > 480) {
        NSLog(@"IndexPath.row: %d, feedItems: %d",indexPath.row, _feedItems.count);
        if((indexPath.row*2+1) <= _feedItems.count) {
            Companies *item = _feedItems[indexPath.row * 2];
            myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.listTableView.bounds) / 2, (item.imageName.size.height / item.imageName.size.width) / 2 * CGRectGetWidth(self.listTableView.bounds))];
            myImageView.tag = indexPath.row*2;
            myImageView.image = item.imageName;

            [myCell addSubview:myImageView];

            if ((indexPath.row*2+1) < _feedItems.count) {
                Companies *item2 = _feedItems[(indexPath.row * 2) + 1];
                myImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.listTableView.bounds) / 2,0,CGRectGetWidth(self.listTableView.bounds) / 2, (item.imageName.size.height / item.imageName.size.width) / 2 * CGRectGetWidth(self.listTableView.bounds))];
                myImageView2.tag = indexPath.row*2+1;
                myImageView2.image = item2.imageName;

                [myCell addSubview:myImageView2];
            }
        } 
    }
    return myCell;
}

我确信我在某些方面有正确的想法,但我所尝试的一切并没有什么不同。我的直觉告诉我,我需要将autoresizingmask添加到myImageView,但是这是在cellForRowAtIndexPath中绘制的。任何帮助是极大的赞赏。

2 个答案:

答案 0 :(得分:3)

如果只需要重新加载单元格,那么对[self.myTableView reloadData]的调用应该会修复它(似乎问题就像滚动问题时一样)。

您可以添加如下内容:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [self.myTableView reloadData]
}

答案 1 :(得分:2)

在iOS 8中,您希望在视图控制器中执行此操作:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    // Resizes the table cell rows to match the new size.
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [self.tableView reloadData];
    } completion:nil];
}