【问题标题】:resizing content UITableviewcell in landscape横向调整内容 UITableviewcell 的大小
【发布时间】:2009-08-31 18:28:49
【问题描述】:

我正在 iPhone SDK 中开发一个应用程序,我想在设备处于横向模式时调整我的 tableviewcell 内容的大小。

在我的 tableviewcell 中,我有一个同步图像和两个标签。在纵向模式下它看起来非常漂亮,但是当我将设备转为横向时,我的 tableview 的内容不会调整大小。

有人可以帮忙吗?

此代码适用于ionterfaceOrientation,如果这是横向的,我将reloadData 调用到我的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);
}

直到一切正常,但我的 tableview 不会调整内容的大小。

这是初始化我的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;
}

标签不响应调整大小,并且在 tableview 上有摆动。我在想的是重置我的表格视图,但我不知道该怎么做。

【问题讨论】:

    标签: uitableview landscape


    【解决方案1】:

    感谢您的分享。这是一个好主意,有一个方法来设置 UITableViewCell。但我发现 uitableviewcell 的内容可以仅使用以下代码行调整:

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

    仅使用 autoresizingMask 行,这允许控件修改其宽度和高度。

    【讨论】:

      【解决方案2】:

      我在自定义 UITableViewCell 时遇到了类似的问题,我最终下载了异步图像(像你一样)并将其设置在 Apple 的 UITableViewCell 提供的 imageView 中(而不是在作为子视图添加的自定义 imageView 中)。

      对于需要调整大小的标签(文本长于纵向一行),我将文本设置为默认 textLabel 并自动调整大小。我还有一个包含日期的第二个标签(文本很短,不需要调整大小),ni将它作为子视图添加到单元格的内容视图中。

      最初我做了你在上面的代码中所做的事情。我设置了我的 uilabels 并将它们添加为子视图,但我发现方向更改警报没有从 tableView 传递给它们,所以在横向看起来不像我预计..

      更多关于 UITableViewCell 你可以在这里找到UITableViewCell

      在我的自定义 UITableView 单元格中,我有一个为单元格设置数据的方法......像这样:

          - (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:)];
      }
      

      在创建单元格后的cellForRowAtIndexPath中:

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

      然后设置单元格将显示的信息:

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

      希望这会对你有所帮助。

      【讨论】:

      • 感谢分享。有一种方法来设置 UITableViewCell 是个好主意。
      【解决方案3】:

      如果您在 IB 中连接了自定义单元格 autoresizingMask = UIViewAutoresizingFlexibleWidth 由于某种原因无法正常工作。

      我在加载视图时调整自定义单元格的大小并禁用视图旋转。此代码适用于 iPhone。我认为 iPad 需要 +256,但从未测试过

      - (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】:

        您可以在shouldAutorotateToInterfaceOrientation 方法上使用这条简单的线:

        self.view.autoresizesSubviews = YES;
        

        【讨论】:

          猜你喜欢
          • 2016-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-09
          • 1970-01-01
          相关资源
          最近更新 更多