- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
编辑:这个答案的解决方案与 iOS7 有时返回 NSIndexPath
而其他时候返回 NSMutableIndexPath
有关。该问题与 begin/endUpdates
并没有真正相关,但希望该解决方案对其他人有所帮助。
所有 - 我在 iOS 7 上运行我的应用程序,我遇到了 UITableView
beginUpdates
和 endUpdates
方法的问题.
我有一个表格 View ,需要在触摸时更改单元格的高度。下面是我的代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// If our cell is selected, return double height
if([self cellIsSelected:indexPath]) {
return 117;
}
// Cell isn't selected so return single height
return 58;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ChecklistItemCell *cell = (ChecklistItemCell *)[self.tableview cellForRowAtIndexPath:indexPath];
[cell.decreaseButton setHidden:NO];
[cell.increaseButton setHidden:NO];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
DLog(@"%@", selectedIndexes);
DLog(@"is selected: %@", isSelected ? @"yes":@"no");
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = @(isSelected);
selectedIndexes[indexPath] = selectedIndex;
[tableView beginUpdates];
[tableView endUpdates];
}
beginUpdates
和 endUpdates
方法的工作非常不一致。 didSelectRowAtIndexPath
方法在每次触摸时都被正确调用(我起初以为 UI 被阻塞),selectedIndexes
正确存储交替值。问题是,有时我触摸一个表格单元格并且所有方法都被正确调用,但单元格高度没有改变。有人知道这是怎么回事吗?
最佳答案
iOS7 中的行为发生了变化,索引路径有时是 NSIndexPath
的实例,有时是 UIMutableIndexPath
的实例。问题是这两个类之间的 isEqual
总是会返回 NO
。因此,您不能可靠地将索引路径用作字典键或在依赖 isEqual
的其他场景中使用。
我能想到几个可行的解决方案:
编写一个始终返回 NSIndexPath
实例的方法,并使用它来生成 key :
- (NSIndexPath *)keyForIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath class] == [NSIndexPath class]) {
return indexPath;
}
return [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
}
通过数据而不是索引路径来识别行。例如,如果您的数据模型是一个 NSString
数组,请将该字符串用作您的 selectedIndexes
映射中的键。如果您的数据模型是 NSManagedObjects
数组,请使用 objectID
等。
我在我的代码中成功地使用了这两种解决方案。
编辑 根据@rob 返回NSIndexPaths
而不是NSStrings
的建议修改解决方案(1)。
关于iOS 7 beginUpdates endUpdates 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18919459/
我有一个带有静态单元格的表格 View 。单击单元格可切换与此单元格关联的选择器 View 单元格的可见性(更改单元格高度)。单击单元格的 tag 属性维护选择器单元格的状态。动画由一系列 begin
情况(使用 Swift 4.0 & > iOS10.0) 我有一个包含部分和行的 TableView。每个细胞只有一个标签并配置为使其高度适应其标签的高度感谢自动布局。 当一个单元格被点击时,它会显示
我没有找到关于这两种方法之间实际潜在区别的很好解释 Control.SuspendLayout和 BeginUpdate (通常出现在列表控件上,如 ListView 、 ComboBox 、 Lis
在处理字段更新时,我需要卡住(停止重绘)DataGridView。 有没有像BeginUpdate这样的东西来临时卡住DataGridView 最佳答案 最好的选择是使用 Adam Robinson
我正在构建一个 UITableView选择时,单元格会“滑出”下方的附加 View 。为此,我使用 [tableview beginUpdates] 为表格 View 中的行高设置了动画。和 [tab
我有一个错误,就是我在自己身上花费了太多时间。基本上,我正在根据 Apple 文档创建一个“ Accordion ” TableView : https://developer.apple.com/l
我的UITableView需要显示来自服务器的一些图像。 UITableViewCell 由单个 UIImageView 和用于 UIImageView 高度的 NSLayoutConstraint
我在每个单元格中使用一个 UITableView 和一个 UITextView 来增加/减少它的高度以适应键入时的文本。为此,我更新了 -textViewDidChange: 委托(delegate)
我正在使用 UITableView 来显示其中包含 UIWebView 的单元格。当 Web View 完成加载时,我通过调用调整单元格的高度 [self.tableView beginUpdates
我在自定义 UITableViewCell 中有一个 UITextView。在扩展 UITextView 时,我希望 UITextView 下方的 subviews 根据其高度下降并增加行高。为此,我
在我的应用程序中,我使用 tableview.begingUpdates() 为我的 tableviewcell 高度(图像的扩展和碰撞)的变化设置动画。它在 iOS 8.0-9.3 上运行最佳。在
编辑:这个答案的解决方案与 iOS7 有时返回 NSIndexPath 而其他时候返回 NSMutableIndexPath 有关。该问题与 begin/endUpdates 并没有真正相关,但希望该
我正在使用 beginUpdates/endUpdates block 对 tableView 进行更改。在整个过程中,我需要更新投影,以便它反射(reflect) tableView 的当前构图。
我有一个 richTextBox,我用它来执行一些语法突出显示。这是一个小型编辑工具,所以我没有编写自定义语法荧光笔 - 相反,我使用 Regex 并使用 Application.Idle 的事件处理
我编写了一个UserControl,其行为类似于ContainerControl,但完全由WindowsForms 绘制(我继承自UserControl) 我想避免在填充控件时绘制控件,因此我需要编写
我的问题是我需要向 WPF 中的 ListBox 添加很多项。在 WinForms 中,您只需使用 BeginUpdate(); 方法,添加所有内容,最后使用 EndUpdate(); 方法。 那么,
我一直在努力解决扩展 UITextView 这个非常普遍的问题。在不断增长的内部 UITableViewCell的 UITableView除了一件小事,我几乎就到了。 我有一个 UITableView
我有什么: 带有单元格的 UITableView。该单元格有一个 UITextView 作为 subview 。所有代表和数据源均已正确设置。单元格高度是从 heightForRow 等正确获取的。用
我使用典型的组合 CoreData - NSFetchedResultsController - UITableViewController。当我使用时: func controllerWillCha
在 tableView 中,我有一个按钮,按下该按钮会修改其下方单元格的高度,以展开和收缩日期选择器,使其出现和消失。但是,我遇到了一个奇怪的动画错误,选择器在关闭和打开时会波动和闪烁。 这是我的 t
我是一名优秀的程序员,十分优秀!