繁体   English   中英

UITableViewCell宽度大于UITableView宽度

[英]UITableViewCell width is greater than UITableView width

我遇到了一个奇怪的问题。 我正在为我的表视图创建一个自定义的选定视图。 但是,此选择视图不太适合。 我发现这是因为我的选择视图是300px,而单元格本身是320px。 奇怪的是, 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;
}

刚设定

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

cellForRowAtIndexPath

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

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

@IvorPrebeg,您不应该在cellForRowAtIndexPath内部设置UITableViewCell的框架。 cell确实执行layoutSubviews之后, UITableViewCell将自动设置为UITableView的宽度。 在插入cell的标签上设置一个值后,将自动完成此操作。

仅根据在cell中插入的字体和文heightForRowAtIndexPath计算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];
}

就像你所看到的, prefferedMaxLayoutWidth的的UILabel我里面UITableViewCell将是0.8 cells宽度(320 * 0.8)。 LayoutSubviews将内部excecuted cellForRowAtIndex建立细胞特性后。 之后, UITableView将调用heightForRowAtIndexPath并计算高度的大小。 因此,我通过传递与cell相同的constrainedToSize (tableview width为320.0 * 0.8)的宽度* 0.8来计算UILabels大小。

示例值适用于iPhone设备,当然,它们将在更大的屏幕上更改。

在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;

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM