- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
几天来我一直对这个问题感到沮丧。我正在尝试将 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/
我是 Swift 开发新手,正在学习许多在线教程。这些教程中的大多数都引用了 Xcode 上的旧版本,并且代码会导致错误。谁能解释为什么下面的代码会产生“UITableViewCell”?无法转换为
我在我的单元格中使用 AutoLayout,但是,当单元格内有大文本并且单元格展开时,单元格似乎与下面的单元格重叠。图像显示了这是如何重叠单元格的: UITableViewCell:
我的函数接受类型为 (cell: UITableViewCell, item: AnyObject) -> () 的函数作为参数。 我的问题是,当我尝试将具有 UITableViewCell 的子类
我正在尝试做一些相当简单的事情,但我看不到最好的方法。我有一个带有两个单元格的 uitableview,第一个在 contextview 中有一个 uitextfield,输入 View 设置为 ui
我在使用 Swift 时遇到了一些挑战。 我有一个带有原型(prototype)单元的 UITableView。有3个单元格前两个包含用户填写的文本字段。第三个包含一个提交按钮。 非常简单! 我的问题
我是 Objective C 的新手。我有一个问题。 我使用自定义的tableviewcell。我正确地使用 JSON 列出了数据。 我想将点击的数据传递到新 View 的自定义表格 View 。 所
我有两个单元格,比方说我的应用程序中的单元格 A 和单元格 B。它们都有自己的类,class CellA : UITableViewCell 和 class CellB : UITableViewCe
我有一个 UIViewController,它根据屏幕上的所需选项实例化一些 UITableViewCell。这些选项之一是“说明”,我创建了一个 .XIB 文件来在屏幕上显示说明。但是,在此屏幕上,
我正在尝试创建一个 tableview,其中有一个 uitableviewcell(cell1) 的子类,其中包含一个按钮等。当在 cell1 中单击该按钮时,该按钮应该在其正下方添加和删除 uita
在使用 UITableView 编码时,我遇到了这个我无法理解的错误。我的表格有 3 行,每行设置 50 像素,但第二行除外,我将其设置为 500 像素,它填满整个屏幕。当我向下滚动到底部时,问题出现
UITableViewCell 反射(reflect)两种不同的状态:突出显示和选定。 对我来说,它们听起来相同,那么到底有什么区别? 最佳答案 突出显示发生在触地时。 选定发生在触摸时,然后调用di
我刚刚添加了屏幕截图。它是定制单元吗?正如我所见,“ map ”是右对齐的,“1800”是左对齐的。我该如何创建它? 最佳答案 它确实看起来像一个自定义单元格。因为 UITableViewCell 继
我不太了解 tableview 的整体行为: 我有一个带有我在 Storyboard中定义的动态单元格(reuseIdentifier:单元格)的表格 View 。除此之外,我还有两个使用两个 nib
我有一个带有以下代码的 UITableViewCell,其中用户单击 UITableViewCell 上的“保存”按钮。单元格中的图像已保存,“保存”按钮动画消失,但单元格仍然存在。 -(UIT
我有一个UITableView,每个单元格都包含一个UIImageView 和一个UIButton。点击按钮后我启动相机,我正在尝试更新该单元格索引路径中的 UIImageView 。到目前为止,这是
有谁知道如何在进入编辑模式时从分组的 UITableView 中隐藏多个单元格?当退出编辑模式时,我希望这些行以动画效果隐藏,就像在“联系人”应用程序中看到的那样。 如您所知,在联系人编辑模式下,行数
我在 UITableViewCell 中有一个 subview ,其中包含一个 UILabel 和一个 UIButton。当我按下按钮时,它出于某种原因取消选择 UITableViewCell。 有没
我一直在经历在 UItableViewCell 中设置 UIDatePicker 的过程,当选择其上方的 UITableViewCell 时会显示该 UIDatePicker,然后在选择 UITabl
我必须集成从代码中以编程方式生成的 UITableViewCell,如下所示: UITableViewCell *newCell = [[UITableViewCell alloc] initWith
我现在正在学习 iOS 开发中的 UITableView 类,我想在导航到多个 UITableViewCells 的部分中实现点击一个 UITableViewCell,该附件类型用于复选标记,我应该怎
我是一名优秀的程序员,十分优秀!