[英]ios app crashes each time i click on the search bar, while retreving data from parse.com
each time i try and click on the search bar, the app keeps crashing, below is the code for the UItableview used with the parse.com SDK, is there something i missed on the code. 每次我尝试点击搜索栏时,应用程序都会崩溃,下面是与parse.com SDK一起使用的UItableview的代码,我在代码中遗漏了什么。 its crashing when the search bar is clicked, before the keyboard gets loaded.
在键盘加载之前单击搜索栏时崩溃。
NSRangeException', reason: ' -[NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' ** First throw call stack: ( 0 CoreFoundation 0x0000000102cb1795 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000102a14991 objc_exception_throw + 43 2 CoreFoundation NSRangeException',原因:' - [NSArrayM objectAtIndex:]:索引0超出空数组的边界'**第一个抛出调用堆栈:(0 CoreFoundation 0x0000000102cb1795 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000102a14991 objc_exception_throw + 43 2 CoreFoundation
this is the view did load method to load search bar 这是视图加载方法加载搜索栏
@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
this is the load for parse 这是解析的负载
- (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;
}
crash log 崩溃日志
2014-01-23 15:58:23.440 MovieTimeLK2[8442:70b] * Terminating app due to uncaught exception 'NSRangeException', reason: ' 2014-01-23 15:58:23.440 MovieTimeLK2 [8442:70b] *由于未捕获的异常'NSRangeException'终止应用程序,原因:' -[ NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' * * First throw call stack: ( 0 CoreFoundation 0x0000000102cb1795 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000102a14991 objc_exception_throw + 43 2 CoreFoundation 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 layoutSubviews] + 207 8 UIKit 0x00000001016d2b27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354 9 QuartzCore 0x00000001010c7a22 -[CALayer layoutSublayers] + 151 10 QuartzCore 0x00000001010bc589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363 11 UIKit 0x00000001016c74
- [ NSArrayM objectAtIndex:]:索引0超出空数组的边界'* *第一次抛出调用堆栈:(0 CoreFoundation 0x0000000102cb1795 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000102a14991 objc_exception_throw + 43 2 CoreFoundation 0x0000000102c578e5 - [__ NSArrayM objectAtIndex:] + 213 3 MovieTimeLK2 0x0000000100005aab - [CarTableViewController的tableView:的cellForRowAtIndexPath:对象:] + 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 layoutSubviews] + 207 8 UIKit 0x00000001016d2b27 - [UIView(CALayerDelegate)layoutSublayersOfLayer:] + 354 9 QuartzCore 0x00000001010c7a22 - [CALayer layoutSublayers] + 151 10 QuartzCore 0x00000001010bc589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363 11 UIKit 0x00000001016c74 70 -[UIView(Hierarchy) 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 becomeFieldEditorForView:] + 725 21 UIKit 0x0000000101c7b2df -[UITextField _becomeFirstResponder] + 143 22 UIKit 0x00000001018e5eb3 -[UISearchBarTextField _becomeFirstResponder] + 92 23 UIKit 0x000000010179fc0c -[UIResponder bec
70 - [UIView(Hierarchy)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 becomeFieldEditorForView:] + 725 21的UIKit 0x0000000101c7b2df - [的UITextField _becomeFirstResponder] + 143 22的UIKit 0x00000001018e5eb3 - [UISearchBarTextField _becomeFirstResponder] + 92 23的UIKit 0x000000010179fc0c - [UIResponder bec omeFirstResponder] + 340 24 UIKit 0x00000001016c6abc -[UIView(Hierarchy) becomeFirstResponder] + 99 25 UIKit 0x0000000101c7ae53 -[UITextField becomeFirstResponder] + 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 sendEvent:] + 910 35 UIKit 0x0000000101686fa2 -[UIApplication sendEvent:] + 211 36 UIKit 0x0
omeFirstResponder] + 340 24的UIKit 0x00000001016c6abc - [UIView的(层次)becomeFirstResponder] + 99 25的UIKit 0x0000000101c7ae53 - [的UITextField becomeFirstResponder] + 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 sendEvent:] + 910 35 UIKit 0x0000000101686fa2 - [UIApplication sendEvent:] + 211 36 UIKit 0x0 000000101674d7f _UIApplicationHandleEventQueue + 9549 37 CoreFoundation 0x0000000102c40ec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 38 CoreFoundation 0x0000000102c40792 __CFRunLoopDoSources0 + 242 39 CoreFoundation 0x0000000102c5c61f __CFRunLoopRun + 767 40 CoreFoundation 0x0000000102c5bf33 CFRunLoopRunSpecific + 467 41 GraphicsServices 0x00000001049873a0 GSEventRunModal + 161 42 UIKit 0x0000000101677043 UIApplicationMain + 1010 43 MovieTimeLK2 0x0000000100006c33 main + 115 44 libdyld.dylib 0x00000001035e65fd start + 1 45 ???
000000101674d7f _UIApplicationHandleEventQueue + 9549 37的CoreFoundation 0x0000000102c40ec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 38的CoreFoundation 0x0000000102c40792 __CFRunLoopDoSources0 + 242 39的CoreFoundation 0x0000000102c5c61f __CFRunLoopRun + 767 40的CoreFoundation 0x0000000102c5bf33 CFRunLoopRunSpecific + 467 41个GraphicsServices 0x00000001049873a0 GSEventRunModal + 161 42的UIKit 0x0000000101677043 UIApplicationMain + 1010 43 MovieTimeLK2 0x0000000100006c33主+ 115 44 libdyld.dylib 0x00000001035e65fd start + 1 45 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
0x0000000000000001 0x0 + 1)libc ++ abi.dylib:以NSException类型的未捕获异常终止(lldb)
From the crash log posted, it seems that your problem is in PFObject *searchUser = [self.SearchResults objectAtIndex:indexPath.row];
从发布的崩溃日志中,您的问题似乎是在
PFObject *searchUser = [self.SearchResults objectAtIndex:indexPath.row];
But that would be strange if you already use return self.SearchResults.count;
但如果你已经使用
return self.SearchResults.count;
in - (NSInteger) Tableview: (UITableView *) Tableview numberOfRowsInSection: (NSInteger) section
in
- (NSInteger) Tableview: (UITableView *) Tableview numberOfRowsInSection: (NSInteger) section
The first thing that comes to my mind is where you call [self.SearchResults removeAllObjects];
我想到的第一件事是你调用
[self.SearchResults removeAllObjects];
in - (void) filterResults: (NSString *)searchTerm
, I suggest using the below code: in
- (void) filterResults: (NSString *)searchTerm
,我建议使用下面的代码:
- (void) filterResults: (NSString *)searchTerm{
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 = [results mutableCopy];
}
Update: 更新:
Since it crashes with the first typing, then make sure that numberOfRowsInSection
returns self.SearchResults.count
not self.objects.count
, if it does but doesn't return zero, try replacing self.searchResults = [NSMutableArray array];
由于它在第一次输入时崩溃,所以请确保
numberOfRowsInSection
返回self.SearchResults.count
而不是self.objects.count
,如果确实但没有返回零,请尝试替换self.searchResults = [NSMutableArray array];
with self.searchResults = [[NSMutableArray alloc] init];
with
self.searchResults = [[NSMutableArray alloc] init];
in your viewDidLoad
在你的
viewDidLoad
Hope it solves your issue. 希望它能解决你的问题。
I think you don't initializate the mutable array change 我认为你没有初始化可变数组更改
self.searchResults = [NSMutableArray array];
to 至
self.searchResults = [NSMutableArray alloc]init];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.