【发布时间】:2014-02-13 19:50:40
【问题描述】:
每次我尝试单击搜索栏时,应用程序都会崩溃,下面是与 parse.com SDK 一起使用的 UItableview 的代码,我在代码中是否遗漏了什么。在加载键盘之前单击搜索栏时它会崩溃。
#
NSRangeException',原因:'-[NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' ** 第一次抛出调用堆栈:( 0 CoreFoundation 0x0000000102cb1795 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000102a14991 objc_exception_throw + 43 2 CoreFoundation
#
这是加载搜索栏的视图加载方法
@interface CarTableViewController () <UISearchDisplayDelegate, UISearchBarDelegate>
{
GADBannerView *bannerView_;
}
@ property (nonatomic, strong) UISearchBar * searchbar;
@ property (nonatomic, strong) UISearchDisplayController *searchcontroller;
@ property (nonatomic, strong) NSMutableArray * SearchResults;
@end
@implementation CarTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//search bar
self.searchbar = [[UISearchBar alloc] initWithFrame: CGRectMake (0, 0, self.view.frame.size.width, 44)];
self.tableView.tableHeaderView = self.searchbar;
self.searchcontroller = [[UISearchDisplayController alloc] initWithSearchBar: self.searchbar contentsController: self];
self.searchcontroller.searchResultsDataSource = self;
self.searchcontroller.searchResultsDelegate = self;
self.searchcontroller.delegate = self;
CGPoint offset = CGPointMake(0, self.searchbar.frame.size.height);
self.tableView.contentOffset = offset;
self.searchResults = [NSMutableArray array];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
这是解析的负载
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
self.parseClassName = @"movienow";
self.textKey = @"mName";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = NO;
}
return self;
}
//where to search and other
- (void) filterResults: (NSString *)searchTerm{
[self.SearchResults removeAllObjects];
PFQuery * query = [PFQuery queryWithClassName: self.parseClassName]; // table in which you are looking for
[query whereKeyExists: @ "mName"]; // column which you are looking for
[query whereKey: @ "mName" containsString : searchTerm];
NSArray * results = [query findObjects];
[self.SearchResults addObjectsFromArray: results];
}
- (BOOL) searchDisplayController:(UISearchDisplayController *) controller shouldReloadTableForSearchString: (NSString *) searchstring {
[self filterResults: searchstring];
return YES;
}
- (NSInteger) Tableview: (UITableView *) Tableview numberOfRowsInSection: (NSInteger) section {
if (Tableview == self.tableView) {
return self.objects.count;
} else {
return self.SearchResults.count;
}
}
- (void) callbackLoadObjectsFromParse: (NSArray *)result error:(NSError *) error {
if (!error) {
[self.SearchResults removeAllObjects],
[self.SearchResults addObjectsFromArray: result];
[self.searchDisplayController.searchResultsTableView reloadData];
} else {
NSLog (@ "Error:%@ %@", error, [error userInfo]);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *CellIdentifier = @"CarTableCell";
CarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CarTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (tableView == self.tableView) {
//cell.mName.text = [object objectForKey:@"objectld"];
NSLog(@"%@",[object objectForKey:@"mName"]);
}
else if (tableView == self.searchDisplayController.searchResultsTableView){
PFObject *searchUser = [self.SearchResults objectAtIndex:indexPath.row];
NSLog(@"%@",[searchUser objectForKey:@"mName"]);
}
// Configure the cell...
return cell;
}
崩溃日志
2014-01-23 15:58:23.440 MovieTimeLK2[8442:70b] * 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“ -[NSArrayM objectAtIndex:]: 索引 0 超出空数组的范围' ** 首先抛出调用堆栈: ( 0 核心基础 0x0000000102cb1795 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000102a14991 objc_exception_throw + 43 2 核心基础 0x0000000102c578e5 -[__NSArrayM objectAtIndex:] + 213 3 MovieTimeLK2 0x0000000100005aab -[CarTableViewController tableView:cellForRowAtIndexPath:object:] + 2155 4 MovieTimeLK2 0x0000000100078c7d -[PFQueryTableViewController tableView:cellForRowAtIndexPath:] + 200 5 UIKit 0x0000000101742b8a-[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348 6 UIKit 0x000000010172a836-[UITableView _updateVisibleCellsNow:] + 2297 7 UIKit 0x000000010173b381-[UITableView 布局子视图] + 207 8 UIKit 0x00000001016d2b27-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354 9 QuartzCore 0x00000001010c7a22 -[CALayer layoutSublayers] + 151 10 石英核心 0x00000001010bc589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363 11 UIKit 0x00000001016c7470-[UIView(层次结构)layoutBelowIfNeeded] + 521 12 UIKit 0x00000001019a46c3-[UISearchDisplayControllerContainerView setFrame:] + 113 13 UIKit 0x000000010199de5f-[UISearchDisplayController setActive:animated:] + 9697 14 UIKit 0x00000001019a0576-[UISearchDisplayController searchBarTextDidBeginEditing:] + 277 15 UIKit 0x00000001018e31b0 -[UISearchBar(UISearchBarStatic)_searchFieldBeginEditing] + 97 16 UIKit 0x00000001016780ae-[UIApplication sendAction:to:from:forEvent:] + 104 17 UIKit 0x0000000101678044-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17 18 UIKit 0x000000010174c450-[UIControl_sendActionsForEvents:withEvent:] + 203 19 UIKit 0x0000000101c83594-[UITextField willAttachFieldEditor:] + 576 20 UIKit 0x00000001017513d7 -[UIFieldEditor 成为FieldEditorForView:] + 725 21 UIKit 0x0000000101c7b2df-[UITextField_becomeFirstResponder] + 143 22 UIKit 0x00000001018e5eb3 -[UISearchBarTextField _becomeFirstResponder] + 92 23 UIKit 0x000000010179fc0c -[UIResponder 成为FirstResponder] + 340 24 UIKit 0x00000001016c6abc -[UIView(Hierarchy) 成为FirstResponder] + 99 25 UIKit 0x0000000101c7ae53 -[UITextField 成为FirstResponder] + 51 26 UIKit 0x000000010198820c-[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 118 27 UIKit 0x0000000101989fc0-[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 1768 28 UIKit 0x000000010198050a _UIGestureRecognizerSendActions + 188 29 UIKit 0x000000010197f470 -[UIGestureRecognizer updateGestureWithEvent:buttonEvent:] + 357 30 UIKit 0x0000000101983829 __UIGestureRecognizerUpdate_block_invoke + 53 31 UIKit 0x00000001019837b1 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 257 32 UIKit 0x000000010197b87d _UIGestureRecognizerUpdate + 93 33 UIKit 0x00000001016ac925-[UIWindow_sendGesturesForEvent:] + 928 34 UIKit 0x00000001016ad5e5 -[UIWindow 发送事件:] + 910 35 UIKit 0x0000000101686fa2-[UIApplication 发送事件:] + 211 36 UIKit 0x0000000101674d7f _UIApplicationHandleEventQueue + 9549 37 核心基础 0x0000000102c40ec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 38 核心基础 0x0000000102c40792 __CFRunLoopDoSources0 + 242 39 核心基础 0x0000000102c5c61f __CFRunLoopRun + 767 40 核心基础 0x0000000102c5bf33 CFRunLoopRunSpecific + 467 41 图形服务 0x00000001049873a0 GSEventRunModal + 161 42 UIKit 0x0000000101677043 UIApplicationMain + 1010 43 电影时间LK2 0x0000000100006c33 主要 + 115 44 libdyld.dylib 0x00000001035e65fd 开始 + 1 45 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)
【问题讨论】:
-
检查你的数组计数,这个崩溃发生在数组中找不到索引
-
searchBar 开始编辑时是否调用了方法?我的猜测是,一旦您开始编辑,它就会在 searchText 为空白的情况下运行查询,因此什么也不返回 - 但仍然返回所以没有错误。显然,数组是空的,所以即使元素 0 也不存在......
-
事实上,根据您的崩溃报告,您似乎确实触发了 searchFieldDidBeginEditing 上的方法...而是触发 searchFieldDidChangeValue (类似的东西)并在传递之前检查返回的数组是否为空它到 searchResults.addObjectsFromArray
标签: ios objective-c uitableview parse-platform