- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我使用 cell.contentView.bounds.size.width
来计算文本字段在 UITableView 单元格中的位置。创建单元格时,调试代码将宽度报告为 302。当单元格滚出屏幕然后重新打开时,调试代码每次都报告宽度为 280。它似乎不想回到 302 并停留在 280。最终结果是文本字段在第二次放入单元格的 contentView 时放置在错误的位置,尽管它被放入第一次就选对了地方。
我认为 22 在某种程度上很重要,但我不知道它是什么。猜测它可能是披露箭头,我将“清除单元格”代码移到宽度确定之前,包括将附件设置为 nada。
谁能告诉我这是怎么回事?
代码(删除了不相关的——据我所知——内容)如下所示:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
while( [cell.contentView.subviews count] ){
id subview = [cell.contentView.subviews objectAtIndex:0];
[subview removeFromSuperview];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
8< snip!
CGFloat theCellWidth = cell.contentView.bounds.size.width - 44.0;
CGFloat theLineHeight = [[UIFont boldSystemFontOfSize: [UIFont labelFontSize]+1.0] lineHeight];
NSLog(@"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width);
if (0==section) {
switch (row) {
case 2:
while( [cell.contentView.subviews count] ){
id subview = [cell.contentView.subviews objectAtIndex:0];
[subview removeFromSuperview];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = @" ";
cell.detailTextLabel.text = @"The Age";
theAgeTextField.frame = CGRectMake(10.0, 2.0, theCellWidth, theLineHeight);
// NSLog(@"cell.contentView %@",cell.contentView);
theAgeTextField.text = theAge;
theAgeTextField.font = [UIFont boldSystemFontOfSize: [UIFont labelFontSize]+1.0];
theAgeTextField.keyboardType = UIKeyboardTypeDecimalPad;
theAgeTextField.borderStyle = UITextBorderStyleNone;
theAgeTextField.userInteractionEnabled = NO;
[cell.contentView addSubview:theAgeTextField];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
8< snip! (lots of closing braces and other stuff omitted)
return cell;
}
男孩女孩们想在家里试试这个吗?
从一个新的基于导航的应用程序开始。将以下代码放入 RootViewController.m 中:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(@"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width);
// Configure the cell.
return cell;
}
实现此目的只需要对默认代码进行两处更改:更改部分中的行数(“return 5”)和单元格样式必须为 UITableViewCellStyleSubtitle。然后,当您运行该程序时,您将看到以下五行:
2011-06-18 11:10:19.976 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
2011-06-18 11:10:19.978 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
2011-06-18 11:10:19.979 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
2011-06-18 11:10:19.980 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
2011-06-18 11:10:19.982 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
将一些单元格拖出屏幕(向上拖动——向下拖动没有任何作用),当它们重新出现时,您会看到:
2011-06-18 11:10:24.013 TestTableCells[7569:207] cell.contentView.bounds.size.width 320
2011-06-18 11:10:24.047 TestTableCells[7569:207] cell.contentView.bounds.size.width 320
2011-06-18 11:10:24.130 TestTableCells[7569:207] cell.contentView.bounds.size.width 320
坦率地说,我对此感到很沮丧,并且非常想花 99 美元(甚至没有可用的应用程序),这样我就可以让 Apple 的人参与其中。
有人知道这里发生了什么吗?
谢谢!
想看看更有趣的东西吗?试试用这个代替 static NSString...
行:
NSString *CellIdentifier = [NSString stringWithFormat: @"%d", arc4random() ];
NSLog(@"%@",CellIdentifier);
现在,每次,日志中的宽度总是 302。这样看来,重用单元格的内容宽度与原始单元格不同。
再次……想知道……有人知道吗?
最佳答案
我怀疑您所看到的是由于更改了单元格的附属 View ,最终会在单元格下次执行 -layoutSubviews
时调整内容 View 的大小。这应该在单元格添加到表格时发生,但在此之前不会更新内容 View 的边界。
无论如何,我不明白为什么这会成为一个问题。您似乎只关心设置 theAgeTextField
的宽度,因此如果您相对于内容 View 的当前宽度适当调整它的大小并设置其 autoresizingMask
标志以使其具有灵 active 宽度,那么当内容 View 的边界发生变化时,它应该根据需要增大或缩小。
如果您需要更详细的布局行为,那么这应该全部发生在 UITableViewCell
子类中。请参阅 -prepareForReuse
和 -layoutSubviews
以获取自定义单元格及其 subview 的机会。您的数据源应该能够将 theAge
传递给您的单元格子类的一个实例,而不用关心它将如何显示的细节。
关于ios - UITableView cell.contentView.bounds.size.width 随着 Cell 重用的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6272009/
将已完成的 MPI_Request 重新用于另一个请求是否安全?我一直在使用 MPI_Request 池来提高性能并且没有错误。但肯定知道会很好。 最佳答案 MPI_Request 类型的变量不是请求
我注意到 Qt 文档在翻译的某些方面不是很冗长。我一直在玩弄它,试图通过反复试验来弄清楚他们的行为。最终目标是在运行时更改翻译,但我很困惑 QTranslator 对象在多大程度上可以重用。 考虑一下
我有一个 UIImageView 对象,它只是一个纯黑色矩形。这是我用来选择 View 中的按钮的方法。 问题是,我的 View 中有 49 个这样的按钮,并且所有这些按钮都可以同时选择。 我用来向按
在 R 中构建模型时,如何保存模型规范以便可以在新数据上重用它?假设我根据历史数据建立逻辑回归,但直到下个月才会有新的观察结果。最好的方法是什么? 我考虑过的事情: 保存模型对象并在新 session
我是 React/Redux 的初学者。 我已经完成了一个基本组件在我的应用程序中,其操作/ reducer /商店运行良好。 我将渲染另一个 具有不同的设置( Prop )。 我想做的是分离这两个组
我正在开发 GUI 纸牌游戏,我想知道是否有办法改进我的代码。这是我的情况。 我有三张牌:A、B 和 C。玩家可以通过分别单击三个按钮之一来更换牌:分别是按钮 1、按钮 2 或按钮 3。 class
每个文本框旁边有 2 个文本框和 2 个按钮 [...]。是否可以使用一个 OpenFileDialog 并将 FilePath 传递到相应的文本框,基于单击哪个按钮?即...如果我单击第一个按钮并打
我有两个场景:第一个场景,渲染纹理平面,第二个场景,应该渲染为纹理。该纹理应用作主场景中平面的贴图。 出于某种原因,所有 THREE.WebGLRenderTarget 示例每帧都会重新绘制两个场景,
我知道 concat、StringBuffer 和 StringBuilder 之间的区别。我知道 StringBuffer.toString 支持数组的内存问题可能会导致内存爆炸。我什至知道 JDK
我有 2 个 Activity 。 A 和 B。A 有一个包含 4 个项目的操作栏。每个项目显示不同的电影列表。 B extends A 因为我希望能够使用操作栏来更改电影列表。 所以我的问题是,当我
我有一个查询,用于检查从搜索文本框中输入的每个关键字,并且必须返回最匹配的关键字。 问题是,我想排除返回行中所有值为 0 的 KW_MATCHED。 SELECT A1.*, (
当方法重用时,是否有像这样的代码可以与 UICollectionViewCell 一起使用? - (UITableViewCell *)tableView:(UITableView *)tableVi
在我的项目中,我想在可 ScrollView 中以zig-zag 模式显示图像。所以我使用 uiscrollview 子查看其中的图像。它工作正常,但它占用了太多内存,因为我将所有图像加载到 Scro
如果我有 UIViewController1 并且我让它以模态方式显示 UIViewController2,但我希望 UIViewController2 显示 UIViewController1 模式
我想在所有 CCMenuItem 中使用完全相同的标签。如果我创建相同的 CCLabelTTF 一次,那么我无法将其添加到多个 CCMenuItem 中,因为它会给出有关已添加标签的运行时错误。但是,
我正在做一个项目,我们需要显示列表与用户位置的距离。为了显示距离,当在输入中给出纬度/经度时,我们使用名为“distance”的脚本字段计算距离 "script_fields" : {
我正在尝试重用我的 UITableViewCells。目前我的应用程序运行良好,在 tableView 中显示内容。然而,当我尝试实现 - 重用 UITableViewCells 时,我的应用程序崩溃
假设我在外部样式表中定义了几个类 .b {font-weight:bold;} .c {text-align:center;} 现在我想要另一个类,它是 b 和 c 的组合 .bc 是否可以使用类 b
我目前经常分配新的协程实例(请参阅我的回答中的代码 here)。 这样做的开销并不小。 我猜想是否有某种方法可以通过重用之前分配的协程来降低成本? 虽然不确定如何实现这一点? 我可以为协程 Alloc
在我的应用程序中,我使用如下代码下载多张图片。这样做是高性能还是我可以以某种方式重用连接? for(int i = 0; i < 100; i++){ URL url = new UR
我是一名优秀的程序员,十分优秀!