gpt4 book ai didi

ios - 为什么 reloadRowsAtIndexPaths 不适用于 iOS 5.0?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:14:38 25 4
gpt4 key购买 nike

已解决:请参阅下面我的回答(和可能的解释)。

我正在制作一款适用于 iOS 5.1 设备但不适用于 iOS 5.0 设备的应用。以下是适用于 5.1 但不适用于 5.0 的故障代码:

- (void) expandIndexPath: (NSIndexPath *) indexPath afterDelay: (BOOL) delay
{
NSIndexPath *oldSelectedIndexPath = [NSIndexPath indexPathForRow:self.mySelectedIndex inSection:0];
self.mySelectedIndex= indexPath.row;
// [self.myTableView beginUpdates];
[self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:self.mySelectedIndex inSection:0], oldSelectedIndexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
// [self.myTableView endUpdates];
}

更奇怪的是,如果我用 [self.myTableView reloadData]; 替换 reloadRowsAtIndexPaths 行,它可以在 iOS 5.0 中运行。为什么是这样? 5.0 是否有关于 reloadRowsAtIndexPaths 行的错误?我已经在 5.0 上尝试过使用和不使用 begin/endUpdates 行,但都不起作用。

编辑:更具体地说,当我在 iOS 5 上运行该应用程序时,它崩溃并出现以下错误:

* Assertion failure in -[_UITableViewUpdateSupport _computeRowUpdates], /SourceCache/UIKit/UIKit-1912.3/UITableViewSupport.m:386 [Switching to process 7171 thread 0x1c03]

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.'

编辑:这是我的 UITableViewDataSource 方法。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//I am positive that this will ALWAYS return the number (it never changes)
return self.myCellControllers.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//I cache my table view cells, thus each time this method gets called it will
// return the exact same cell. Yes, I know that most of the time I should be dequeing
// and reusing cells; just trust me that this time, it's best for me to cache them
// (There are very few cells so it doesn't really matter)
CellController *controller = [self.myCellControllers objectAtIndex:indexPath.row];
return controller.myCell;
}

- numberOfSectionsInTableView: always returns 1;

唯一有趣的方法是

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == self.mySelectedIndex)
{
return DEFAULT_CELL_HEIGHT + self.expandSize;
}
else
{
return DEFAULT_CELL_HEIGHT;
}
}

正如快速概览这部分程序的工作原理一样,当用户点击一个单元格时,该单元格会滚动到屏幕顶部。在它位于屏幕顶部之后,它的索引被设置为 self.mySelectedIndex 并且它展开(变高)。

最佳答案

我找到了一个有效的解决方案。如果我将 expandIndexPath 方法更改为此它有效:

- (void) expandIndexPath: (NSIndexPath *) indexPath afterDelay: (BOOL) delay {
[self.myTableView beginUpdates];
NSIndexPath *oldSelectedIndexPath = [NSIndexPath indexPathForRow:self.mySelectedIndex inSection:0];
self.mySelectedIndex= indexPath.row;
if(oldSelectedIndexPath.row >= 0)
{
[self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:self.mySelectedIndex inSection:0],oldSelectedIndexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}
else
{
[self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:self.mySelectedIndex inSection:0],nil] withRowAnimation:UITableViewRowAnimationNone];
}
[self.myTableView endUpdates];
}

据我所知,问题在于 oldSelectedIndexPath 有时被设置为负行值。因此,当我尝试用负数行重新加载 indexPath 时,它在 iOS 5.0 中崩溃了。看来 iOS 5.1 修复了这个问题并做了更多的错误检查。

关于ios - 为什么 reloadRowsAtIndexPaths 不适用于 iOS 5.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11549620/

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