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