ios7 indexPathForCell 的坑(真是一個大大的坑)

筆者在編寫APP 有一個功能點擊cell上一個button,修改cell的在tableview中的位置ios

在ios8上沒有問題。app

在ios7上老是崩潰ui

如下是崩潰後提示:google

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (11) must be equal to the number of rows contained in that section before the update (11), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).spa

開始懷疑是刪除或插入indexpath 有問題 。google了下,解決方法各式各樣。.net

後來斷點調試發如今ios7上調試

[self.tableviewindexPathForCell:myCell] 返回了nilblog

在ios8中使用了get

- (void)selecttop:(id)sender{it

    if ([sender isKindOfClass:[UIButtonclass]]) {

        UIView *view = sender;

        AppListCell *myCell = (AppListCell *)view.superview.superview;

        NSIndexPath * cellPath = [self.tableviewindexPathForCell:myCell];

}

google下是ios7 上cell上面還多了一個UITableViewWrapperView 具體能夠參見:http://blog.csdn.net/u013604612/article/details/23120615

stackoverflaw 中 http://stackoverflow.com/questions/19282304/uitableview-indexpathforcell-ios6-v-ios7

有一個很好的處理方法

 

- (void)selecttop:(id)sender{

    if ([sender isKindOfClass:[UIButtonclass]]) {

        UIView *view = sender;

        while (![view isKindOfClass:[UITableViewCellclass]]) {

            view = [view superview];

        }

        AppListCell *myCell = (AppListCell *)view;

        NSIndexPath * cellPath = [self.tableviewindexPathForCell:myCell];

}

標記下

真的是個坑!!!

聽說ios6上也是兩個superview 就能夠獲取到!!!!

相關文章
相關標籤/搜索