gpt4 book ai didi

ios - 检索 UITableViewCell 框架

转载 作者:可可西里 更新时间:2023-11-01 03:09:13 25 4
gpt4 key购买 nike

几天来我一直对这个问题感到沮丧。我正在尝试将 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 方法返回绘制单元格的框架的宽度,这对于普通样式表来说很好,因为单元格宽度是框架的整个宽度。但是,在分组表中,单元格的宽度小于绘制它的框架的宽度,因此此方法将返回一个比单元格的宽度宽很多的数字。分组表格样式中的单元格宽度可能是小于表格框架宽度的固定数字,因此这可能是解决问题的方法。如果是这样的话,我会调查并在这里回答我自己的问题。

最佳答案

我已经确定了自己的答案,希望它能帮助面临同样问题的任何人。我在 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];
}

关于ios - 检索 UITableViewCell 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7652922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com