Iphone 从详细视图返回时tableView崩溃

Iphone 从详细视图返回时tableView崩溃,iphone,cocoa-touch,uitableview,Iphone,Cocoa Touch,Uitableview,在我的应用程序中,我有一个表视图和一个详细视图。表视图是从数据库查询填充的。我在viewdiload方法中有这个数据库方法(我不知道还能放在哪里),然后在视图中有[self.tableView reload]方法。问题是,当我从详细视图返回到表视图并单击相同的元素(在表视图中)时,应用程序崩溃。我猜这是因为当我从细节视图返回并单击单元格时,数据库方法仍在运行。因此,永远不会重新加载表视图中的数据。你们知道怎么解决这个问题吗 *编辑: *以下是数据库中的代码,我使用它填充表视图:* 编辑:我注意到

在我的应用程序中,我有一个表视图和一个详细视图。表视图是从数据库查询填充的。我在
viewdiload
方法中有这个数据库方法(我不知道还能放在哪里),然后在
视图中有
[self.tableView reload]
方法。问题是,当我从详细视图返回到表视图并单击相同的元素(在表视图中)时,应用程序崩溃。我猜这是因为当我从细节视图返回并单击单元格时,数据库方法仍在运行。因此,永远不会重新加载表视图中的数据。你们知道怎么解决这个问题吗

*编辑: *以下是数据库中的代码,我使用它填充表视图:*

编辑:我注意到,当我返回到表视图时,如果我单击下一个元素,应用程序不会崩溃,但是如果我单击表视图中的上一个元素(行),应用程序会崩溃

编辑:我将代码更改为
viewdide
方法,并修复了内存泄漏问题<代码>自我。课程
(非原子,保留)
以下是代码:

- (void)viewWillAppear:(BOOL)animated
{
    //Convert the course id to string 
    //NSString *selectedCategoryIdConverted = [NSString stringWithFormat:@"%d", self.selectedCategory._id];

    NSMutableArray *tempArray = [[NSMutableArray alloc]init];

    // Set up sqlite statement
    sqlite3_stmt *dbStatement = nil;

    NSString *sqlQuery = [NSString stringWithFormat:@"SELECT co.name, co.id, o.location FROM course as co JOIN categories as ca JOIN occasions as o WHERE co.catID = ca.id AND co.catID = %i AND o.courseID = co.id", self.selectedCategory._id];

    //Convert the query string to const char
    const char *sqlQueryConverted =[sqlQuery UTF8String];

    int prepareSqlQuery = sqlite3_prepare_v2( [[DatabaseController sharedDatabaseController] getDb], sqlQueryConverted, -1, &dbStatement, NULL);

    //Run the query
    while ( sqlite3_step(dbStatement) == SQLITE_ROW ) 
    {    

        const char *name = (const char *)sqlite3_column_text(dbStatement, 0);
        int courseId = sqlite3_column_int(dbStatement, 1);
        const char *location = (const char *)sqlite3_column_text(dbStatement, 2);

        //Convert the returnedElement char to string
        NSString *nameConverted = [[[NSString alloc] initWithUTF8String:name] autorelease];
        NSString *locationConverted = [[[NSString alloc] initWithUTF8String:location] autorelease];

        Course *course = [[Course alloc] initWithName:nameConverted _id:courseId location:locationConverted];


        [tempArray addObject:course];
        [course release];
        course = nil;

    }    

    self.courses = tempArray;
    [tempArray release];

    [self.tableView reloadData];    

    [super viewWillAppear:animated];
}
编辑:以下是错误日志:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceWhiteColor _id]: unrecognized selector sent to instance 0x4d2e2a0'
Call stack at first throw:
(
    0   CoreFoundation                      0x010435a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01197313 objc_exception_throw + 44
    2   CoreFoundation                      0x010450bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00fb4966 ___forwarding___ + 966
    4   CoreFoundation                      0x00fb4522 _CF_forwarding_prep_0 + 50
    5   BookApp                             0x000079e8 -[CoursesCategoriesViewController viewWillAppear:] + 184
    6   UIKit                               0x0017c9be -[UINavigationController _startTransition:fromViewController:toViewController:] + 858
    7   UIKit                               0x0017732a -[UINavigationController _startDeferredTransitionIfNeeded] + 266
    8   UIKit                               0x0017e562 -[UINavigationController pushViewController:transition:forceImmediate:] + 932
    9   UIKit                               0x001771c4 -[UINavigationController pushViewController:animated:] + 62
    10  BookApp                             0x000056d6 -[CoursesViewController tableView:didSelectRowAtIndexPath:] + 374
    11  UIKit                               0x00135b68 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
    12  UIKit                               0x0012bb05 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    13  Foundation                          0x0084579e __NSFireDelayedPerform + 441
    14  CoreFoundation                      0x010248c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
    15  CoreFoundation                      0x01025e74 __CFRunLoopDoTimer + 1220
    16  CoreFoundation                      0x00f822c9 __CFRunLoopRun + 1817
    17  CoreFoundation                      0x00f81840 CFRunLoopRunSpecific + 208
    18  CoreFoundation                      0x00f81761 CFRunLoopRunInMode + 97
    19  GraphicsServices                    0x012d81c4 GSEventRunModal + 217
    20  GraphicsServices                    0x012d8289 GSEventRun + 115
    21  UIKit                               0x000ccc93 UIApplicationMain + 1160
    22  BookApp                             0x00001ff9 main + 121
    23  BookApp                             0x00001f75 start + 53
)

terminate called after throwing an instance of 'NSException'

你的代码正在泄漏内存。将临时数组分配给self.courses后,您不会发布该版本。此外,在将课程对象添加到临时数组后,也不会释放该对象。对于SQLite操作,我建议您使用FMDB。将SQL查询放在每个视图控制器中以从数据库中获取数据后,管理起来将非常容易。而且,遵循这种模式也是一种不好的做法。将FMDB用于所有sqlite用途。为所有sqlite通信创建一个单独的类。

从堆栈跟踪中,有趣的部分是:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UIDeviceWhiteColor\u id]:未识别的选择器发送到实例0x4d2e2a0'

5 BookApp 0x000079e8-[CourseScategories视图控制器视图将出现:][184

对应于:
-(void)视图将显示:(BOOL)动画{
//... 
NSString*sqlQuery=
[NSString stringWithFormat:
@“从课程中选择co.name、co.id、o.location作为共同加入类别作为ca加入场合,其中co.catID=ca.id和co.catID=%i和o.courseID=co.id”,

自我选择的类别。\u id];//您在控制台中检查过了吗?您收到了什么错误消息?您是对的,db也是我的第一印象。可能是您可以在详细信息视图的dealloc方法中取消db操作。最可能的原因是,当您存储了数据库中的数据时,您没有保留它。如果您没有保留,请发布ViewDiLoad方法确定是否是这样。请查看编辑。我在控制台中没有收到任何错误。问题在于“DidSelectRowatineXpath”方法中,我正在释放传递给“self.selectedCategory”的categoryId。我已删除了“categoryId release”'行,它正在工作。但是,这不会导致内存泄漏吗?我不知道您正在使用
categoryId
做什么,这取决于您需要什么。释放后,可以将答案设置为
categoryId
nil
,以避免向已发布的实例发送消息(这是一个很好的做法,我建议在任何地方都使用,一个
[x release]
后面总是跟一个
x=nil
)或者答案可能是只有在您不再需要时才使用release var,更可能是在
dealoc
中。。。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceWhiteColor _id]: unrecognized selector sent to instance 0x4d2e2a0'
Call stack at first throw:
(
    0   CoreFoundation                      0x010435a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01197313 objc_exception_throw + 44
    2   CoreFoundation                      0x010450bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00fb4966 ___forwarding___ + 966
    4   CoreFoundation                      0x00fb4522 _CF_forwarding_prep_0 + 50
    5   BookApp                             0x000079e8 -[CoursesCategoriesViewController viewWillAppear:] + 184
    6   UIKit                               0x0017c9be -[UINavigationController _startTransition:fromViewController:toViewController:] + 858
    7   UIKit                               0x0017732a -[UINavigationController _startDeferredTransitionIfNeeded] + 266
    8   UIKit                               0x0017e562 -[UINavigationController pushViewController:transition:forceImmediate:] + 932
    9   UIKit                               0x001771c4 -[UINavigationController pushViewController:animated:] + 62
    10  BookApp                             0x000056d6 -[CoursesViewController tableView:didSelectRowAtIndexPath:] + 374
    11  UIKit                               0x00135b68 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
    12  UIKit                               0x0012bb05 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    13  Foundation                          0x0084579e __NSFireDelayedPerform + 441
    14  CoreFoundation                      0x010248c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
    15  CoreFoundation                      0x01025e74 __CFRunLoopDoTimer + 1220
    16  CoreFoundation                      0x00f822c9 __CFRunLoopRun + 1817
    17  CoreFoundation                      0x00f81840 CFRunLoopRunSpecific + 208
    18  CoreFoundation                      0x00f81761 CFRunLoopRunInMode + 97
    19  GraphicsServices                    0x012d81c4 GSEventRunModal + 217
    20  GraphicsServices                    0x012d8289 GSEventRun + 115
    21  UIKit                               0x000ccc93 UIApplicationMain + 1160
    22  BookApp                             0x00001ff9 main + 121
    23  BookApp                             0x00001f75 start + 53
)

terminate called after throwing an instance of 'NSException'
- (void)viewWillAppear:(BOOL)animated {
    //... 
    NSString *sqlQuery =
   [NSString stringWithFormat:
      @"SELECT co.name, co.id, o.location FROM course as co JOIN categories as ca JOIN occasions as o WHERE co.catID = ca.id AND co.catID = %i AND o.courseID = co.id",
      self.selectedCategory._id]; // << The culprit
    //...