UITableView滚动崩溃

时间:2010-01-13 01:34:21

标签: iphone objective-c uitableview iphone-sdk-3.0 scroll

我目前正在开发一个iPhone应用程序,它从RSS源加载数据并将其显示在2个UITableViews中的Tab Bar应用程序中,我们称之为TableViewA和TableViewB。在AppDelegate中,我们有以下方法:

- (void)getDataWithContext:(NSManagedObjectContext *)context
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSError *parseError = nil;

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    XMLReader *xmlReader = [[[XMLReader alloc] initWithContext:context]autorelease];
    [xmlReader parseXMLFileAtURL:[NSURL URLWithString:@"http://example.com/rss.xml"] parseError:&parseError];
    [pool release];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

在我们的applicationDidFinishLaunching:方法中,我们调用与该选择器分离一个新线程:

if ([self isDataSourceAvailable] == NO) {
    UIAlertView *noConnection = [[[UIAlertView alloc] initWithTitle:@"Connection Unavailable" message:@"The connection to the database is unavailable. The information displayed may be outdated." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [noConnection show];
}
else{
    [NSThread detachNewThreadSelector:@selector(getDatawithContext:) toTarget:self withObject:context];
}

TableViewControllerA是一个UITableViewController

使用以下方法加载TableView的数据和图像。当上面的XML阅读器的线程退出时,将所有条目放入CoreData后,我们会收到通知,并将条目从CoreData重新加载到TableView中。

TableViewControllerB是一个TableViewControllerA,它继承了这些相同的方法,并进行了一些更改以从数据库中选择不同的条目。

- (IBAction)loadData: (id) sender{
    BroadwayAppDelegate *appDelegate = (BroadwayAppDelegate *) [[UIApplication sharedApplication] delegate];

    checkDate = [NSPredicate predicateWithFormat: @"date <= %@",
                        [NSDate date]];
    if ( [self.showsSegmentedControl selectedSegmentIndex] == UISegmentedControlNoSegment || 
            [self.showsSegmentedControl selectedSegmentIndex] == 0){
        self.listContent = [CoreDataHelper searchObjectsInContext :@"Entry" :self.checkDate :@"title" :YES :appDelegate.managedObjectContext];
    }
    else if ([self.showsSegmentedControl selectedSegmentIndex] == 1){
        self.listContent = [CoreDataHelper searchObjectsInContext :@"Entry" :self.checkDate :@"startDate" :NO :appDelegate.managedObjectContext];
    }
    else if ([self.showsSegmentedControl selectedSegmentIndex] == 2){
        self.listContent = [CoreDataHelper searchObjectsInContext :@"Entry" :self.checkDate :@"endDate" :YES :appDelegate.managedObjectContext];
    }
    else if ([self.showsSegmentedControl selectedSegmentIndex] == 3){
        self.listContent = [CoreDataHelper searchObjectsInContext :@"Entry" :self.checkDate :@"type" :YES :appDelegate.managedObjectContext];
    }

    // create a filtered list that will contain products for the search results table.
    self.filteredContent = [NSMutableArray arrayWithCapacity:[self.listContent count]];

    // restore search settings if they were saved in didReceiveMemoryWarning.
    if (self.savedSearchTerm)
    {
        [self.searchDisplayController setActive:self.searchWasActive];
        [self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
        [self.searchDisplayController.searchBar setText:savedSearchTerm];

        self.savedSearchTerm = nil;
    }

    NSError *error;
    [appDelegate.managedObjectContext save:&error];


    [self.tableView reloadData];

}

 - (void)viewDidLoad {
     [super viewDidLoad];
     [self loadData:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(threadExited) name:@"NSThreadWillExitNotification" object:nil];

     dateFormatter = [[NSDateFormatter alloc]init];
     [dateFormatter setDateStyle: NSDateFormatterMediumStyle];
     self.title = @"Entries";

 }

- (void)threadExited{
    [self loadData:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [NSThread detachNewThreadSelector:@selector(loadImages) toTarget:self withObject:nil];
}
- (void) loadImages{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSArray *coreDataEntries = [CoreDataHelper getObjectsFromContext:@"Entry" :@"title" :NO :appDelegate.managedObjectContext];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    for (Entry *s in coreDataEntries){
        if (s.image == nil) {
            NSString *URLString = [[Entry imageURLFromLink:s.link withExtension:@".jpg"]absoluteString];
            NSURL *imageURL = [NSURL URLWithString:URLString];
            NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
            s.image = [UIImage imageWithData:imageData];
        }
    }
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self.tableView reloadData];
    [pool release];
}

如果用户在重新加载数据时滚动tableview,或者可能在XML读取器退出时滚动,则应用程序崩溃。为什么会发生这种情况,我们该如何解决?我们使用提供的工具来检查内存泄漏和那种事情,我们没有发现任何与崩溃相关的内容。

1 个答案:

答案 0 :(得分:1)

使用调试模式运行!当应用程序崩溃时,单击“显示调试器”按钮

alt text http://gyazo.com/b279117e3619711c94c7c13626d835ae.png

并查看问题

或查看video