【问题标题】:Retrieving a UITableViewCell frame检索 UITableViewCell 框架
【发布时间】:2015-07-16 06:26:54
【问题描述】:

这几天我对这个问题感到很沮丧。我正在尝试将 UILabel 添加到 UITableViewCell。我希望 UILabel 跨越单元格的整个宽度,左右两侧为负 5 或 10 以供外观。我的问题是以编程方式确定放置标签的单元格框架的大小。无论我使用哪种 UITableViewStyle,cell.contentVew.frame.size.width 值都远不及单元格框架本身的宽度。

例如,在我正在构建的表中,我可以通过继承 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 )。

我需要访问什么值才能返回单元格绘图框的整个宽度?我敢肯定,就像大多数令人烦恼的问题一样,这个解决方案会让我想在额头上扇自己一巴掌,但我现在已经做好了准备。

编辑以获取更多信息:

对任何感兴趣的人的澄清。我的这个问题主要与分组样式表有关。对于简单的样式,我上面的问题的答案可以简单地在 cellForRowAtIndexPath 方法中通过以下方式确定:

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

我遇到的问题是 rectForRowAtIndexPath 方法返回绘制单元格的框架的宽度,这对于普通样式表来说很好,因为单元格宽度是框架的整个宽度。但是,在分组表中,单元格的宽度小于绘制它的框架的宽度,因此此方法将返回一个比单元格宽度宽很多的数字。分组表格样式中单元格的宽度可能是小于表格框架宽度的固定数字,因此这可能是解决问题的方法。如果是这种情况,我将对此进行调查并在这里回答我自己的问题。

【问题讨论】:

  • 很好的问题,我得到了完全相同的行为,这让我抓狂。我的解决方法是不考虑帧大小,只参考内容视图大小百分比的子视图宽度。我很确定内容视图框架不包括为附件保留的单元格左侧和右侧的区域(移动、删除、披露等),但仍然没有回答为什么单元格框架总是报告错误大小的问题.希望有人会来了解这一切。
  • @Bartosz - 在我的示例中,我从肖像中获取数字。
  • @Rog - 我在上面编辑了我的问题,以包含可能对您有所帮助的更多信息。
  • @Chris 你的纵向单元格怎么可能比 320 宽?你从哪里得到的 397 宽度?
  • an0 - 很抱歉没有说清楚,这个应用是为 iPad 设计的。

标签: ios uitableview


【解决方案1】:

我已经确定了自己的答案,我希望它可以帮助任何面临同样问题的人。我在this StackOverflow answer.上找到的一个分组tableView的边距计算

此代码将在 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];
    }

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多