gpt4 book ai didi

iOS 7 beginUpdates endUpdates 不一致

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

编辑:这个答案的解决方案与 iOS7 有时返回 NSIndexPath 而其他时候返回 NSMutableIndexPath 有关。该问题与 begin/endUpdates 并没有真正相关,但希望该解决方案对其他人有所帮助。


所有 - 我在 iOS 7 上运行我的应用程序,我遇到了 UITableView beginUpdatesendUpdates 方法的问题.

我有一个表格 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];

}

beginUpdatesendUpdates 方法的工作非常不一致。 didSelectRowAtIndexPath 方法在每次触摸时都被正确调用(我起初以为 UI 被阻塞),selectedIndexes 正确存储交替值。问题是,有时我触摸一个表格单元格并且所有方法都被正确调用,但单元格高度没有改变。有人知道这是怎么回事吗?

最佳答案

iOS7 中的行为发生了变化,索引路径有时是 NSIndexPath 的实例,有时是 UIMutableIndexPath 的实例。问题是这两个类之间的 isEqual 总是会返回 NO。因此,您不能可靠地将索引路径用作字典键或在依赖 isEqual 的其他场景中使用。

我能想到几个可行的解决方案:

  1. 编写一个始终返回 NSIndexPath 实例的方法,并使用它来生成 key :

    - (NSIndexPath *)keyForIndexPath:(NSIndexPath *)indexPath
    {
    if ([indexPath class] == [NSIndexPath class]) {
    return indexPath;
    }
    return [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
    }
  2. 通过数据而不是索引路径来识别行。例如,如果您的数据模型是一个 NSString 数组,请将该字符串用作您的 selectedIndexes 映射中的键。如果您的数据模型是 NSManagedObjects 数组,请使用 objectID 等。

我在我的代码中成功地使用了这两种解决方案。

编辑 根据@rob 返回NSIndexPaths 而不是NSStrings 的建议修改解决方案(1)。

关于iOS 7 beginUpdates endUpdates 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18919459/

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