【发布时间】:2012-07-17 23:24:30
【问题描述】:
已解决:请参阅下面的答案(以及可能的解释)。
我正在制作一个适用于 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];
}
更奇怪的是,如果我将reloadRowsAtIndexPaths 行替换为[self.myTableView reloadData];,它在iOS 5.0 中也能正常工作。为什么是这样? 5.0 是否有关于 reloadRowsAtIndexPaths 行的错误?我已经在 5.0 上尝试过,有和没有 begin/endUpdates 行,但都不起作用。
编辑:更具体地说,当我在 iOS 5 上运行应用程序时,它会崩溃并出现以下错误:
*
-[_UITableViewUpdateSupport _computeRowUpdates],/SourceCache/UIKit/UIKit-1912.3/UITableViewSupport.m:386中的断言失败 [切换到进程7171线程0x1c03]由于未捕获的异常'
NSInternalInconsistencyException'而终止应用程序,原因:'无效的表视图 更新。应用程序已请求更新表视图 与数据源提供的状态不一致。'
编辑:这是我的 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 并扩展(变得更高)。
【问题讨论】:
-
始终将
-beginUpdates/-endUpdates方法与-reloadRowsAtIndexPaths:withRowAnimation:结合使用 -
定义“不适合”。什么都没发生?抛出异常?用户被设备发出的激光束蒸发了?
-
大声笑...抱歉没有更具体。它崩溃并出现以下错误:
Assertion failure in -[_UITableViewUpdateSupport _computeRowUpdates], /SourceCache/UIKit/UIKit-1912.3/UITableViewSupport.m:386 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.' -
您需要发布您的
numberOfSectionsInTableView:、tableView:numberOfRowsInSection:和tableView:cellForRowAtIndexPath:方法。
标签: ios objective-c uitableview cocoa-touch ios5