UITableViewCell宽度大于UITableView宽度

时间:2012-10-23 07:38:35

标签: objective-c ios uitableview

我遇到了一个奇怪的问题。我正在为我的tableviews创建自定义选定视图。但是,此选择视图不适合。我发现这是因为我的选择视图是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;
}

4 个答案:

答案 0 :(得分:3)

只需设置

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

@IvorPrebeg,您不应该为UITableViewCell内的cellForRowAtIndexPath设置框架。在UITableViewCell执行UITableView后,cell将自动设置为layoutSubviews的宽度。这将在您在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];
}

您可以看到,我prefferedMaxLayoutWidth内的UILabel的{​​{1}}将是UITableViewCell宽度的0.8(320 * 0.8)。设置单元格属性后,cells将在LayoutSubviews内完成。之后,cellForRowAtIndex将调用UITableView并计算高度的大小。因此,我通过为heightForRowAtIndexPath(tableview width为320.0 * 0.8)传递宽度* 0.8来计算UILabels大小,与constrainedToSize相同。

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

答案 3 :(得分:0)

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

 }
相关问题