屏幕旋转后,无法调整UITableViewCell中图像的大小。在我cellForRowAtIndexPath
本人检测装置的宽度,而如果宽度小于480我设置图像到屏幕宽度,然后将高度设置为所述(图像高度/图像宽度*屏幕宽度),以保持缩放的图像。如果设备的宽度大于480,我将执行相同的操作,除了将宽度设置为屏幕的一半,使缩放后的图像略小。
下面是我绘制的纵向和横向布局页面的图像。当我从横向旋转到纵向时,可见图像仍保留在屏幕上,并且除非我滚动到它们离开屏幕的位置,否则不要调整大小。
当我从肖像转到风景时,同样的事情也起作用,它在右侧留下了一个很大的开放空间,直到我向下滚动为止,然后加载就很好了。我觉得这很简单,例如添加一个自动调整大小的蒙版,但是我不知道该放在哪里以及如何解决它:
[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;
}
我确定在某个时候我有正确的想法,但是我尝试过的一切都没有改变。我的直觉告诉我我需要在myImageView中添加autoresizingmask,但是这是在cellForRowAtIndexPath中绘制的。任何帮助是极大的赞赏。
[self.myTableView reloadData]
如果只需要重新加载单元格,则应调用来修复该问题(似乎是问题所在,因为滚动问题时已修复)。
您可以添加如下所示的内容:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.myTableView reloadData]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句