问题描述
编辑:
此答案的解决方案与iOS7有关,有时会返回 NSIndexPath
,有时会返回 NSMutableIndexPath
。该问题与 begin / endUpdates
无关,但希望该解决方案能够帮助其他人。
Edit:The solution for this answer is related to iOS7 sometimes returning NSIndexPath
and other times returning NSMutableIndexPath
. The issue wasn't really related to begin/endUpdates
, but hopefully the solution will help some others.
全部 - 我在iOS 7上运行我的应用程序,我遇到了 beginUpdates
和<$ c $的问题c> endUpdates UITableView
的方法。
All - I'm running my app on iOS 7, and I'm running into problems with the beginUpdates
and endUpdates
methods for a UITableView
.
我有一个需要的tableview触摸时更改单元格的高度。下面是我的代码:
I have a tableview that needs to change the height of a cell when touched. Below is my code:
- (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
正确存储交替值。问题是,有时我触摸表格单元格并且所有方法都被正确调用,但单元格高度不会改变。有人知道发生了什么吗?
The beginUpdates
and endUpdates
methods are working pretty inconsistently. The didSelectRowAtIndexPath
method gets called correctly on each touch(I thought at first the UI was getting blocked), and the selectedIndexes
is storing alternating values correctly. The issue is, sometimes I touch a table cell and all the methods are called properly, but the cell height doesn't change. Anyone know what's going on?
推荐答案
iOS7中的行为发生了变化,其中索引路径有时是<$ c $的实例c> NSIndexPath 和其他时间 UIMutableIndexPath
。问题是这两个类之间的 isEqual
总是会返回 NO
。因此,您无法可靠地将索引路径用作字典键,或者在依赖 isEqual
的其他方案中使用。
There is a change in behavior in iOS7 where index paths are sometimes instances of NSIndexPath
and other times UIMutableIndexPath
. The problem is that isEqual
between these two classes is always going to return NO
. Thus, you cannot reliably use index paths as dictionary keys or in other scenarios that rely on isEqual
.
我可以想想几个可行的解决方案:
I can think of a couple of viable solutions:
-
编写一个总是返回
NSIndexPath 并使用它来生成密钥:
Write a method that always returns an instance of
NSIndexPath
and use it to generate keys:
- (NSIndexPath *)keyForIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath class] == [NSIndexPath class]) {
return indexPath;
}
return [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
}
按数据识别行,而不是索引路径。例如,如果您的数据模型是 NSString
的数组,请将该字符串用作 selectedIndexes
映射的键。如果您的数据模型是 NSManagedObjects
的数组,请使用 objectID
等。
Identify rows by the data, not the index path. For example, if your data model is an array of NSString
, use that string as the key into your selectedIndexes
map. If your data model is an array of NSManagedObjects
, use the objectID
, etc.
我在我的代码中成功使用了这两种解决方案。
I'm successfully using both of these solutions in my code.
编辑修改后的解决方案(1)基于@ rob建议返回 NSIndexPaths
而不是 NSStrings
。
EDIT Modified solution (1) based on @rob's suggestion of returning NSIndexPaths
rather than NSStrings
.
这篇关于iOS 7 beginUpdates endUpdates不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!