2012-07-18 82 views
2

已解答:请参阅下面的答案(和可能的解释)。为什么reloadRowsAtIndexPaths不适用于iOS 5.0?

我正在制作适用于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]; 
} 

更奇怪的是,它在安装iOS 5.0如果我[self.myTableView reloadData];更换reloadRowsAtIndexPaths线。为什么是这样? 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,并且它展开(变高)。

+0

始终将'-beginUpdates' /'-endUpdates'方法与'-reloadRowsAtIndexPaths:withRowAnimation:' – CodaFi 2012-07-18 20:24:47

+0

一起使用定义“不适用于”。什么都没发生?异常抛出?用户从设备发出的激光束蒸发了吗? – benzado 2012-07-18 20:25:00

+0

大声笑...抱歉没有更具体。它崩溃,出现以下错误:' - 断言失败 - [_ UITableViewUpdateSupport _computeRowUpdates],/SourceCache/UIKit/UIKit-1912.3/UITableViewSupport.m:386 终止应用程序由于未捕获的异常'NSInternalInconsistencyException',原因:'无效的表视图更新。该应用程序已请求更新与表格视图不一致的数据源提供的状态。'' – Nosrettap 2012-07-18 20:27:21

回答

2

我发现了一个可行的解决方案。如果我改变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]; 
} 

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

相关问题