【问题标题】:UITableViewCell width is greater than UITableView widthUITableViewCell 宽度大于 UITableView 宽度
【发布时间】:2018-05-17 19:11:44
【问题描述】:

我遇到了一个奇怪的问题。我正在为我的表格视图创建一个自定义选定视图。但是,此选择视图不太适合。我发现这是因为我的选择视图是 300 像素,而单元格本身是 320 像素。奇怪的是UITableView 的宽度实际上只有 300px。如何调整表格的单元格宽度?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *tableIdentifier = @"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];

  if (cell == nil)
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier] autorelease];


  if (tableView == bandTable)
  {
    UIImageView *imageBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"column1_selected.png"]];
    [imageBg setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    [cell setSelectedBackgroundView:imageBg];
    [imageBg release];

    NSArray *array = [bandDictionary objectForKey:@"Bands"];

    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    NSLog(@"CELL: %f", cell.bounds.size.width);
  }
  return cell;
}

【问题讨论】:

  • 请粘贴您的代码 CellForIndexPath:
  • 如果您不确定数据的宽度,而不是尝试使用 UIView 来支撑它,您可以使用 UIScrollView 并确保根据您的设置其 contentSize 属性需要。您需要向右或向左滚动才能看到剩余(或隐藏)的数据。
  • @Hemang 这不是我关心的数据。这是自定义选择视图不太合适。就像我说的那样,表格和自定义选择视图的宽度都是 300 像素,但单元格宽度是 320 像素。
  • 你不能明确地将 imageBg 的框架设置为 imageBg.frame = CGRectMake....
  • 您能以编程方式检查表格大小吗?我认为cellForRowAtIndexPath 将是这个检查的好地方。

标签: objective-c ios uitableview


【解决方案1】:

设置好了

cell.frame = CGRectMake(0,
                        0,
                        self.tableView.frame.size.width,
                        cell.frame.size.height);

cellForRowAtIndexPath.

【讨论】:

  • 谢谢。我的牢房溢出来了,就这样
【解决方案2】:

在创建单元格后添加这两行,

[[cell contentView] setFrame:[cell bounds]];
[[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

【讨论】:

    【解决方案3】:

    @IvorPrebeg,您不应该在cellForRowAtIndexPath 中设置UITableViewCell 的框架。在cell 执行layoutSubviews 之后,UITableViewCell 将自动设置为UITableView 的宽度。这将在您插入cell 的标签上设置值后自动完成。

    heightForRowAtIndexPath 内部计算一个大小非常重要,该大小基于插入到cell 中的字体和文本,该大小等于单元格内的大小。就我而言,它看起来像这样:

    - (CGFloat)tableView:(UITableView *)tableView 
               heightForRowAtIndexPath:(NSIndexPath *)indexPath{+
        ChatMessage *message = 
         [self.fetchedResultsController objectAtIndexPath:indexPath];
    
        CGSize size = [message.text sizeWithFont:self.messageCell.labelMessage.font
                           constrainedToSize:CGSizeMake(self.tableView.frame.size.width * 0.8f, HUGE_VAL)
                               lineBreakMode:NSLineBreakByWordWrapping];
    
        return size.height;
    }
    

    比里面的UITableViewCell:

    - (void)layoutSubviews{
        [super layoutSubviews];
    
        if( _fInitWidth == 0 ){
            self.labelMessage.preferredMaxLayoutWidth = 
               self.bounds.size.width * 0.8f;
        }    
        [self.contentView setNeedsDisplay];
    }
    

    如您所见,UILabel 中的prefferedMaxLayoutWidth 在我的UITableViewCell 中将是cells 宽度的0.8 (320 * 0.8)。设置单元格属性后,LayoutSubviews 将在cellForRowAtIndex 内执行。之后,UITableView 将调用heightForRowAtIndexPath 并计算高度的大小。因此,我通过为constrainedToSize(tableview 宽度为 320.0 * 0.8)传递与cell 相同的宽度 * 0.8 来计算 UILabels 的大小。

    示例值适用于 iPhone 设备,当然它们会在更大的屏幕上发生变化。

    【讨论】:

      【解决方案4】:

      在 C# 上:

      public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath){
      
        UITableViewCell cell = new UITableViewCell();
        cell.Frame = new CoreGraphics.CGRect(0, 0,this.selectSchoolTableview.Frame.Width, cell.Frame.Size.Height);
        cell.ContentView.AutosizesSubviews = true;
        return cell;
      }
      

      另一个技巧:

      public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath){
      
      UITableViewCell cell = new UITableViewCell();
      cell.Accessory = UITableViewCellAccessory.None;
      cell.ContentView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
      cell.ContentView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight;
      return cell;
      
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多