Objective-C问题:cellForRowAtIndexPath崩溃

时间:2010-06-09 21:56:43

标签: iphone objective-c

我希望用户能够在数据库中搜索记录。获取和返回的结果完美无缺。我很难设置UItableview来显示结果。应用程序不断在cellForRowAtIndexPath崩溃。在我心脏病发作之前,请有人帮忙。谢谢。

@implementation SearchViewController
@synthesize mySearchBar;
@synthesize textToSearchFor;
@synthesize myGlobalSearchObject;
@synthesize results;
@synthesize tableView;
@synthesize tempString;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark -
#pragma mark Table View
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //handle selection; push view
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
/*  if(nullResulSearch == TRUE){
        return 1;
    }else {
        return[results count];
    }   
 */ 
    return[results count];
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;  // Test hack to display multiple rows.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Search Cell Identifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
    }

    NSLog(@"TEMPSTRING %@", tempString);
    cell.textLabel.text = tempString;

    return cell;    
 }

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload {
    self.tableView = nil;
}

- (void)dealloc {
    [results release];
    [mySearchBar release];
    [textToSearchFor release];
    [myGlobalSearchObject release];
    [super dealloc];
}

#pragma mark -
#pragma mark Search Function & Fetch Controller

- (NSManagedObject *)SearchDatabaseForText:(NSString *)passdTextToSearchFor{
    NSManagedObject *searchObj;
    UndergroundBaseballAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c]%@", passdTextToSearchFor]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entry" inManagedObjectContext:managedObjectContext]; 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];   

    [request setSortDescriptors:sortDescriptors];
    [request setEntity: entity]; 
    [request setPredicate: predicate]; 

    NSError *error;

    results = [managedObjectContext executeFetchRequest:request error:&error];

    if([results count] == 0){
        NSLog(@"No results found");
        searchObj = nil;
        nullResulSearch == TRUE;
    }else{
        if ([[[results objectAtIndex:0] name] caseInsensitiveCompare:passdTextToSearchFor] == 0) {
            NSLog(@"results %@", [[results objectAtIndex:0] name]);
            searchObj = [results objectAtIndex:0];
            nullResulSearch == FALSE;
        }else{
            NSLog(@"No results found");
            searchObj = nil;
            nullResulSearch == TRUE;
        }       
    }

    [tableView reloadData]; 

    [request release];
    [sortDescriptors release];

    return searchObj;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    textToSearchFor =  mySearchBar.text;
    NSLog(@"textToSearchFor: %@", textToSearchFor);
    myGlobalSearchObject = [self SearchDatabaseForText:textToSearchFor];
    NSLog(@"myGlobalSearchObject: %@", myGlobalSearchObject);
    tempString = [myGlobalSearchObject valueForKey:@"name"];
    NSLog(@"tempString: %@", tempString);   
}
@end



*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UILongPressGestureRecognizer isEqualToString:]: unrecognized selector sent to instance 0x3d46c20'

1 个答案:

答案 0 :(得分:4)


问题可能与引用计数有关。

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar方法中,尝试

self.tempString = [myGlobalSearchObject valueForKey:@"name"];   

(假设tempString设置为保留)

OR

tempString = [[myGlobalSearchObject valueForKey:@"name"] retain];


我还建议做一些检查零值。即如果[myGlobalSearchObject valueForKey:@"name"] == nil?

,该怎么办?