簡體   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