[英]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.