2011-10-04 47 views
8

我已经对这个问题感到沮丧了几天。我试图添加一个UILabel到UITableViewCell。我希望UILabel跨越单元格的整个宽度,在左右两侧减去5或10以获得外观。我的问题是以编程方式确定放置标签的单元格框架的大小。无论使用哪个UITableViewStyle,cell.contentVew.frame.size.width值都不在单元框架本身的宽度附近。检索UITableViewCell框架

例如,在我构建的表,我可以实现通过继承的UITableViewCell和带手动确定宽度创建一个UILabel(通过公正的审判和错误)经我想要的结果:

CGRectMake(10, 12, 397, self.contentView.frame.size.height); 

但它的那令我烦恼的397号码。我想要一种以编程方式确定它应该用于任何宽度表格或样式的方法。这应该是一个简单的过程,只需确定整个单元格的宽度,然后减去10或20,这样UILabel的边缘实际上不会触及单元格的边缘。

但是,如果我设置tableViewStyle到UITableViewStyleDefault然后尝试:

NSLog(@"Width: %f", self.contentView.frame.size.width); 

我得到320。如果我的样式设置为任何其他三种风格,返回的数字是302甚至320数字不是靠近单元格框架的宽度(与我手动确定的397个数字一样)。

我需要访问什么值才能返回单元格绘图框架的整个宽度?我相信,就像最令人头痛的问题一样,这个解决方案会让我想要额头上的自拍,但我现在已经准备好了。

编辑更多信息:

对任何有兴趣的人一个澄清。我的这个问题主要涉及分组样式表。

CGFloat cellWidth = [tableView rectForRowAtIndexPath:indexPath].size.width; 

时遇到的问题是,rectForRowAtIndexPath方法返回,其中所述细胞是框架的宽度:对于一个普通的风格,回答我的问题上述能够简单地cellForRowAtIndexPath方法中通过确定由于单元格宽度是框架的整个宽度,所以对于普通样式表格来说这很好。但是,在分组表格中,单元格的宽度小于绘制框架的宽度,因此此方法将返回一个比单元格宽度稍宽的数字。组合表格样式中的单元格的宽度可能小于表格框架的宽度,因此这可能是解决问题的方法。如果是这种情况,我会调查并回答我自己的问题。

+0

伟大的问题我得到完全相同的行为,它驱使我坚果。我的解决方法是不考虑帧大小,只是参考内容视图大小百分比的子视图宽度。我很确定内容视图框架排除了为附件保留的单元格的左侧和右侧区域(移动,删除,披露等),但仍然没有回答为什么单元格框架总是报告错误大小的问题。希望有人会来,并理解这一切。 – Rog

+0

这是在风景? –

+0

@Bartosz - 在我的例子中,我从肖像中获取数字。 – Chris

回答

8

我已经确定了我自己的答案,我希望它能帮助任何人面临同样的问题。一个分组的tableView的余量的计算我上this StackOverflow answer.

此代码发现将提供跨越与所述单元的两个边缘之间的裕度的小区的的tableView小区内的标签,并在细胞内垂直居中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UILabel *label; 
    CGFloat groupedStyleMarginWidth, tableViewWidth; 
    UIFont *labelFont = [UIFont boldSystemFontOfSize:17.0]; // Set to whatever you like 
    NSString *labelText = @"Test String"; 

    // Calculate the margin between the cell frame and the tableView 
    // frame in a grouped table view style. 
    tableViewWidth = tableView.frame.size.width; 
    if (tableView.style == UITableViewStyleGrouped) { 
     if (tableViewWidth > 20) 
      groupedStyleMarginWidth = (tableViewWidth < 400) ? 10 : MAX(31, MIN(45, tableViewWidth*0.06)); 
     else 
      groupedStyleMarginWidth = tableViewWidth - 10; 
    } 
    else 
     groupedStyleMarginWidth = 0.0; 

    if (cell == nil) { 
     CGRect tableViewRect; 
     CGRect labelRect; 
     CGFloat x, y, w, h, labelMargin; 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     // Retrieve the rect of the table view. 
     tableViewRect = [tableView rectForRowAtIndexPath:indexPath]; 

     // Set whatever margin around the label you prefer. 
     labelMargin = 10; 

     // Determine rect values for the label. 
     x = tableRect.origin.x + labelMargin; 

     // Calculate width of label 
     w = tableRect.size.width - (groupedStyleMargin * 2) - (labelMargin * 2); 

     // Calculate height of table based on font set earlier. 
     h = [labelText sizeWithFont:font].height; 

     // Calculate y position for the label text baseline to center 
     // vertically within the cell. 
     y = (tableRect.origin.y/2) - (h/4); 

     labelRect = CGRectMake(x, y, w, h); 

     label = [[UILabel alloc] initWithFrame:labelRect]; 
     label.text = labelText; 
     label.tag = 0; 
     [cell.contentView addSubview:stepLabel]; 
     [label release]; 
    } 
    else { 
     label = (UILabel *)[cell viewWithTag:0]; 
    } 
1

这样的声音最好现在由自动布局约束来处理。