简体   繁体   English

UITableViewCell宽度大于UITableView宽度

[英]UITableViewCell width is greater than UITableView width

I encountered a weird problem. 我遇到了一个奇怪的问题。 I am creating a custom selected view for my tableviews. 我正在为我的表视图创建一个自定义的选定视图。 However, this selection view doesn't fit well. 但是,此选择视图不太适合。 I found out that this is because my selection view is 300px while the cell itself is 320px. 我发现这是因为我的选择视图是300px,而单元格本身是320px。 The weird part is that the UITableView 's width is actually just 300px only. 奇怪的是, UITableView的宽度实际上仅为300px。 How will I adjust the cell width of my table? 如何调整表格的单元格宽度?

- (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;
}

Just set 刚设定

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

in cellForRowAtIndexPath . cellForRowAtIndexPath

Add this two lines after your cell gets created, 创建单元格后,添加这两行,

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

@IvorPrebeg, you shouldn't set the frame for the UITableViewCell inside cellForRowAtIndexPath . @IvorPrebeg,您不应该在cellForRowAtIndexPath内部设置UITableViewCell的框架。 The UITableViewCell will automatically be set to the width of the UITableView after the cell did execute layoutSubviews . cell确实执行layoutSubviews之后, UITableViewCell将自动设置为UITableView的宽度。 This will be done automatically after setting a value on the label you inserted in the cell . 在插入cell的标签上设置一个值后,将自动完成此操作。

Its just important to calculate inside heightForRowAtIndexPath a size based on the font and the text inserted into the cell which is equal to the one inside your cell. 仅根据在cell中插入的字体和文heightForRowAtIndexPath计算heightForRowAtIndexPath内部的大小,该大小与cell中的大小相等才是重要的。 In my case it looks like this: 就我而言,它看起来像这样:

- (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;
}

And than inside the UITableViewCell : 而且比在UITableViewCell内部:

- (void)layoutSubviews{
    [super layoutSubviews];

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

Like you can see, the prefferedMaxLayoutWidth of the UILabel inside my UITableViewCell will be 0.8 of the cells width (320 * 0.8). 就像你所看到的, prefferedMaxLayoutWidth的的UILabel我里面UITableViewCell将是0.8 cells宽度(320 * 0.8)。 LayoutSubviews will be excecuted inside cellForRowAtIndex after setting up the cells properties. LayoutSubviews将内部excecuted cellForRowAtIndex建立细胞特性后。 After that, UITableView will call the heightForRowAtIndexPath and calculate the size for the height. 之后, UITableView将调用heightForRowAtIndexPath并计算高度的大小。 Therefore I calculate the UILabels size by passing a width * 0.8 for constrainedToSize ( tableview width is 320.0 * 0.8 ) the same as in the cell . 因此,我通过传递与cell相同的constrainedToSize (tableview width为320.0 * 0.8)的宽度* 0.8来计算UILabels大小。

The example values are for iPhone devices, they will change on bigger screens of course. 示例值适用于iPhone设备,当然,它们将在更大的屏幕上更改。

On C#: 在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;
}

Another trick: 另一个技巧:

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