简体   繁体   English

当我触摸UITableView中的特定单元时,应用崩溃

[英]App crash when I touch specific cell in UITableView

I have Maintableview (a UIViewController ) with contents and detailview (a UIViewController ) that show detail for each of cells in Maintableview. 我有MaintableviewUIViewController ),其中包含内容和detailviewUIViewController ),它们显示了Maintableview中每个单元的详细信息。
I have added a "favorite" button in " detailview " that user can add to Favoritetableview (a UIViewController ) and every thing work fine. 我在“ detailview ”中添加了一个“收藏夹”按钮,用户可以将其添加到FavoritetableviewUIViewController )中,并且一切正常。

Now I have added "add to favorite" in Maintableview with swipe to left. 现在,我在Maintableview添加了“添加到收藏夹”, Maintableview滑动。 It adds contents successfully to Favoritetableview but when I touch that cell in Favoritetableview the app crashes. 它将内容成功添加到Favoritetableview但是当我在Favoritetableview触摸该单元时,应用程序崩溃。

Below is the console logs for the crash: 以下是崩溃的控制台日志:

 Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010a871d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a2e3deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010a75a934 -[__NSArrayI objectAtIndex:] + 164
    3   iranbirds                           0x00000001075cf672 -[FavoriteTableViewController prepareForSegue:sender:] + 530
    4   UIKit                               0x00000001090c55d5 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 369
    5   UIKit                               0x00000001090c5433 -[UIStoryboardSegueTemplate _perform:] + 82
    6   UIKit                               0x0000000108b1b5f8 -[UIViewController performSegueWithIdentifier:sender:] + 99
    7   iranbirds                           0x00000001075cf42d -[FavoriteTableViewController tableView:didSelectRowAtIndexPath:] + 189
    8   UIKit                               0x0000000108ac51c6 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1887
    9   UIKit                               0x0000000108ac541b -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 388
    10  UIKit                               0x0000000108989f62 _runAfterCACommitDeferredBlocks + 317
    11  UIKit                               0x000000010899de4c _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
    12  UIKit                               0x00000001089aa147 _afterCACommitHandler + 90
    13  CoreFoundation                      0x000000010a796c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    14  CoreFoundation                      0x000000010a796ba7 __CFRunLoopDoObservers + 391
    15  CoreFoundation                      0x000000010a78c7fb __CFRunLoopRun + 1147
    16  CoreFoundation                      0x000000010a78c0f8 CFRunLoopRunSpecific + 488
    17  GraphicsServices                    0x000000010be6bad2 GSEventRunModal + 161
    18  UIKit                               0x000000010897df09 UIApplicationMain + 171
    19  iranbirds                           0x00000001075d32ef main + 111
    20  libdyld.dylib                       0x000000010ba6492d start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

favorite table view: 最喜欢的表格视图:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *indexPath = (NSIndexPath *)sender;
    Favorite *fav = (Favorite *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString *combinedName = fav.name;

    if  ([segue.identifier isEqualToString:@"FavoriteBirdDetail"])
    {
        GeneralViewController *detailViewController = (GeneralViewController*)[segue destinationViewController];

        detailViewController.birdName = [combinedName componentsSeparatedByString:@"^"][0];;
        detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];;
        detailViewController.managedOjbectContext = self.managedOjbectContext;
    }

}

This line cause error: 此行导致错误:
detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];

Any help would be appreciated. 任何帮助,将不胜感激。

detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];

Well, look at this code - you are getting second object from an array by subscript syntax. 好吧,看一下这段代码-您正在通过下标语法从数组中获取第二个对象。 And the code would work if an array contains at least 2 items. 如果数组包含至少2个项目,该代码将起作用。

It looks like your array contains zero or one element, that is why app crashes when you try to get an element by 1 index - [1] , which means the second element. 数组似乎包含零个或一个元素,这就是为什么当您尝试通过1索引[1]表示第二个元素时,应用崩溃的原因。

To prevent crash you should check an array count property before: 为了防止崩溃,您应该在以下情况下检查数组count属性:

NSArray *anArray = [combinedName componentsSeparatedByString:@"^"]; 

if anArray.count >= 2 {
    detailViewController.sciName = anArray[1];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我长按 UITableView 自定义标题单元格时应用程序崩溃 - App crashes when I long press on UITableView Custom Header Cell UITableView细胞删除崩溃? - UITableView cell deletion crash? 为什么UITableView在我触摸数据时显示数据? - why UITableView is showing data when I touch it? 更新UITableView时,新建可见行的缺失单元格崩溃 - Crash with Missing cell for newly visible row when updating UITableView 当单元被“回收”时,重用UITableView单元会导致崩溃 - Reusing UITableView Cells causes crash when a cell is “Recycled” 使用自定义单元格在UITableView中展开UILabel时,Swift崩溃 - Swift crash when unwrapping UILabel in UITableView with custom cell 当我在iOS7上触摸UISearchBar时,为什么我的UISearchBar和我的UITableView Cell Header之间存在差距? - Why do I get a gap between my UISearchBar and my UITableView Cell Header when I touch the UISearchBar on iOS7? 当我在滚动UITableview期间显示文档目录中的图像时,内存不断增加然后应用程序崩溃 - Memory continuously increase then app crash when i display image from document directory during scrolling UITableview UISearchDisplayController和UITableView原型单元崩溃 - UISearchDisplayController and UITableView prototype cell crash 当一个UITableview的单元格内的另一个UITableView的单元格内的UITextView时,触摸检测 - Touch detection when UITextView inside cells of a UITableview inside cell of another UITableView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM