UITableViewCell Image not resizing after rotation
I am having trouble resizing images in my UITableViewCell after the screen rotates. In my cellForRowAtIndexPath
I detect the width of the device, and if the width is less than 480 I set the image to the screens width, and then set the height to the (images height / images width * screen width) to keep the image scaled. If the width of the device is greater than 480 I do the same thing, except I set the width equal to half of the screen, making the scaled image a little bit smaller.
Below is an image I drew of how the page looks with both portrait and landscape layout. When I rotate from landscape to portrait the visible images still remain on the screen and do not re-size unless I scroll to the point where they leave the screen.
The same thing works when I go from portrait to landscape, it leaves a big open space on the right side until I scroll down, then it loads just fine. I have a feeling this is something simple such as adding an autoresizingmask, but I cannot figure out where to put it and how to address it:
[self.myTableView setAutoresizesSubviews:YES];
[self.myTableView setAutoresizingMask:
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight];
Below is my 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;
}
I am certain I had the right idea at some point, but everything I have tried has not made a difference. My gut feeling tells me I need to add the autoresizingmask to the myImageView, however that is drawn in the cellForRowAtIndexPath. Any help is greatly appreciated.
A call to [self.myTableView reloadData]
should fix it if the cells only need to be reloaded (seems to be the problem as when you scroll the issue is fixed).
You can add something which looks like this:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.myTableView reloadData]
}
在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];
}
链接地址: http://www.djcxy.com/p/58386.html
上一篇: 旋转到不同方向时约束不起作用