当我触摸 UITableView 中的特定单元格时应用程序崩溃
App crash when I touch specific cell in UITableView
我有 Maintableview
(a UIViewController
) 的内容和 detailview
(a UIViewController
) 显示 Maintable 中每个单元格的详细信息查看。
我在“detailview”中添加了一个 "favorite" 按钮,用户可以将其添加到 Favoritetableview
(a UIViewController
) 并且一切正常。
现在,我已通过向左滑动在 Maintableview
中添加了 "add to favorite"。
它成功地将内容添加到 Favoritetableview
,但是当我触摸 Favoritetableview
中的那个单元格时,应用程序崩溃了。
以下是崩溃的控制台日志:
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
最喜欢的 table 视图:
- (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;
}
}
此行导致错误:
detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];
如有任何帮助,我们将不胜感激。
detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];
好吧,看看这段代码 - 您正在通过下标语法从数组中获取第二个对象。如果数组至少包含 2 个项目,则代码将起作用。
您的数组似乎包含零个或一个元素,这就是为什么当您尝试通过 1 索引获取元素时应用程序崩溃的原因 - [1]
,这意味着第二个元素。
为防止崩溃,您应该在 count
属性 之前检查数组:
NSArray *anArray = [combinedName componentsSeparatedByString:@"^"];
if anArray.count >= 2 {
detailViewController.sciName = anArray[1];
}
我有 Maintableview
(a UIViewController
) 的内容和 detailview
(a UIViewController
) 显示 Maintable 中每个单元格的详细信息查看。
我在“detailview”中添加了一个 "favorite" 按钮,用户可以将其添加到 Favoritetableview
(a UIViewController
) 并且一切正常。
现在,我已通过向左滑动在 Maintableview
中添加了 "add to favorite"。
它成功地将内容添加到 Favoritetableview
,但是当我触摸 Favoritetableview
中的那个单元格时,应用程序崩溃了。
以下是崩溃的控制台日志:
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
最喜欢的 table 视图:
- (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;
}
}
此行导致错误:
detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];
如有任何帮助,我们将不胜感激。
detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];
好吧,看看这段代码 - 您正在通过下标语法从数组中获取第二个对象。如果数组至少包含 2 个项目,则代码将起作用。
您的数组似乎包含零个或一个元素,这就是为什么当您尝试通过 1 索引获取元素时应用程序崩溃的原因 - [1]
,这意味着第二个元素。
为防止崩溃,您应该在 count
属性 之前检查数组:
NSArray *anArray = [combinedName componentsSeparatedByString:@"^"];
if anArray.count >= 2 {
detailViewController.sciName = anArray[1];
}