resizing content UITableviewcell in landscape

此生再无相见时 提交于 2019-12-22 11:17:04

问题


I'm developing an app in iPhone SDK and I want to resize the content of my tableviewcell when the device is in landscape mode.

In my tableviewcell I have an synchronous image and two labels. In portrait mode it looks very nice, but when I turn the device in landscape the content of my tableview doesn't resize.

There is someone can help with this?

This code is for ionterfaceOrientation and if this is in landscape I call the reloadData to my tableview.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (interfaceOrientation == UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight)
    {

        NSLog(@"LandsCape Mode");
        [mytabla reloadData];

    }else{
        NSLog(@"Portrait Mode");
    }



    return YES;
    // for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

Until this everything is OK, but my tableview does not resize content.

This is the code init my tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath

 AsyncImageView *asyncImageView = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        }

            CGRect frame;
            frame.origin.x = 5;
            frame.origin.y = 10;
            frame.size.width = 70;
            frame.size.height = 60;
            asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
            asyncImageView.tag = ASYNC_IMAGE_TAG;
            cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            frame.origin.x = 52 + 10;
            frame.size.width = 200;

            NSString *urlSeccion = aBook.Titulo;
            urlSeccion = [urlSeccion stringByReplacingOccurrencesOfString:@"\n" withString:@""];

            NSString *SeccionSummary = aBook.Summary;
            SeccionSummary = [SeccionSummary stringByReplacingOccurrencesOfString:@"\n" withString:@""];

            [cell.contentView addSubview:asyncImageView];

            upLabelText = [[UILabel alloc] initWithFrame:CGRectMake(90, cell.frame.origin.y, cell.frame.origin.size, 50)];
            upLabelText.textColor = [UIColor blackColor];
            upLabelText.text = urlSeccion;
            [upLabelText setNumberOfLines:2];
            [upLabelText setLineBreakMode:UILineBreakModeWordWrap];
            upLabelText.font = [UIFont systemFontOfSize:13.0];  


            downLabelText = [[UILabel alloc] initWithFrame:CGRectMake(90, 45, cell.frame.origin.size, 50)];
            downLabelText.textColor = [UIColor blackColor];
            downLabelText.text = SeccionSummary;
            [downLabelText setNumberOfLines:5];
            [downLabelText setLineBreakMode:UILineBreakModeWordWrap];
            downLabelText.font = [UIFont systemFontOfSize:8.0];  



            [cell.contentView addSubview:downLabelText];
            [cell.contentView addSubview:upLabelText];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

            asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];



            NSString *urlSeccionImagen = aBook.ThumbnailURL;
            urlSeccionImagen = [urlSeccionImagen stringByReplacingOccurrencesOfString:@" " withString:@""];
            urlSeccionImagen = [urlSeccionImagen stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            urlSeccionImagen = [urlSeccionImagen stringByReplacingOccurrencesOfString:@" " withString:@""];

            NSString *urlString = [NSString stringWithFormat:urlSeccionImagen,indexPath.row + 1];
            NSURL *url = [NSURL URLWithString:urlString];
            [asyncImageView loadImageFromURL:url];

return cell;
}

The labels don't respond to the resize and there are swing over the tableview. What I'm thinking is to reset my tableview but I don't know how to do it.


回答1:


Thanks for this sharing. This is a great idea having a method to set-up the UITableViewCell. But I figured out that the content of a uitableviewcell can rezise with just this code line:

tituloLabelText.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
tituloLabelText.text = @"A Large text resides here";
[tituloLabelText setNumberOfLines:2];
[tituloLabelText setLineBreakMode:UILineBreakModeWordWrap];

With just the line of autoresizingMask this allow to the controlls modify their Widht and Height.




回答2:


i had a similar problem with a custom UITableViewCell and i ended up downloading async the image(like you) and setting it in the imageView provided by Apple's UITableViewCell(not in a custom imageView added as a subview).

For the label that needed to be resized(the text was longer than one row in portrait) i set the text in the default textLabel and it was resized automatically. I also had a second label that contained a date(the text was short and didn't needed resizing),ni added it as a subView to the content view of the cell.

Initially i did what you did in the code above.. i setup my uilabels and added them as subviews but i discovered that the orientation changes alerts didn't pass to them from the tableView so in landscape didn't looked as i expected..

more about the UITableViewCell you can find here UITableViewCell

in my custom UITableView cell i had a method that set-up the data for the cell...something like this:

    - (void) setData:(Feed *) dict
{
    self.autoresizesSubviews = YES;
    self.imageView.image = [UIImage newImageFromResource:@"noFeedImg.png"];
    self.textLabel.highlightedTextColor = [UIColor whiteColor];
    [[self textLabel] setOpaque:NO];
    [[self textLabel] setBackgroundColor:[UIColor clearColor]];
    [[self textLabel] setText:[dict feedTitle]];
    [[self textLabel] setTextColor:[UIColor blackColor]];
    [[self textLabel] setNumberOfLines:3];
    [[self textLabel] setFont:[UIFont boldSystemFontOfSize:12]];

    [self addSubview:dateLabel];
    dateLabel.text = [dict publishDate];
    dateLabel.frame = CGRectMake(self.contentView.bounds.origin.x + 125, 100, 175, 14);
    self.URL = [dict feedURL];

    [imageLoader loadImageFromURL:[NSURL URLWithString:@"http://www.google.com"] withCallbackTarget:self withCallbackSelector:@selector(setupImage:)];
}

in the cellForRowAtIndexPath after creating the cell:

FeedTableViewCell *cell = [[[FeedTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

and after that setup the info that the cell will display:

[cell setData:[feedsList objectAtIndex:feedIndex]];

hope this will help you.




回答3:


If you have custom cell hooked up in the IB autoresizingMask = UIViewAutoresizingFlexibleWidth does not work for some reason.

I resize my custom cells when the view is loaded and disable view rotation. This code works on iPhone. iPad needs +256 I think, but never tested

- (void)viewDidLoad
  {
    [super viewDidLoad];

    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
    CGRect newFrame = customCell.frame;
    newFrame.size.width = newFrame.size.width+160;
    customCell.frame = newFrame;
  }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}



回答4:


You can use this simple line on the shouldAutorotateToInterfaceOrientation method :

self.view.autoresizesSubviews = YES;


来源:https://stackoverflow.com/questions/1358757/resizing-content-uitableviewcell-in-landscape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!