2011-01-06 3 views
0

일부 카테고리 (CategoryViewController)가있는 테이블보기가있는 간단한 iOS 앱을 개발 중입니다. 이 카테고리 중 하나를 클릭하면보기가 래서 피가있는 다른 테이블보기가있는 RecipesListController로 전달됩니다. 이 조리법은 클릭 한 카테고리에 따라 다른 plist에서로드됩니다.iOS TableView 충돌로 다른 데이터로드

처음 범주를 클릭하면 요리법 목록이로드되고 올바르게 표시됩니다. 카테고리 목록으로 돌아가 카테고리 중 하나를 클릭하면 (다시 똑같이) 앱이 다운됩니다. 그리고 어떻게해야할지 모르겠다. viewWillAppear은 올바르지 만 충돌이 발생한 후에 발생합니다.

도와 주시겠습니까? 전체 프로젝트가 필요한 경우 우편으로 처리 할 수 ​​있습니다. 승인? 여기

가 CategoryViewControoler.m

#import "CategoryViewCotroller.h" 
#import "NavAppDelegate.h" 
#import "RecipesListController.h" 


@implementation CategoryViewController 

@synthesize recipeCategories; 



- (void)viewDidLoad { 

    // Create the categories 
    NSArray *array = [[NSArray alloc] initWithObjects:@"Antipasti", @"Focacce", @"Primi", @"Secondi", @"Contorni", @"Dolci", nil]; 
    self.recipeCategories = array; 
    [array release]; 

    // Set background image 
    UIImageView *bgImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sfondo_app.png"]]; 
    [self.tableView setBackgroundView:bgImg]; 
    [bgImg release]; 

    [self.tableView reloadData]; 
    [super viewDidLoad]; 
} 



- (void)viewDidUnload { 

    self.recipeCategories = nil; 
// [childController release]; 
    [super viewDidUnload]; 
} 



- (void)dealloc { 

    [recipeCategories release]; 
// [childController release]; 
    [super dealloc]; 
} 




#pragma mark - 
#pragma mark Table data source methods 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [recipeCategories count]; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellId = @"RecipesCategoriesCellId"; 

    // Try to reuse a cell or create a new one 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:CellId] autorelease]; 
    } 

    // Get the right value and assign to the cell 
    NSUInteger row = [indexPath row]; 
    NSString *rowString = [recipeCategories objectAtIndex:row]; 
    cell.textLabel.text = rowString; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    [rowString release]; 

    return cell; 
} 



#pragma mark - 
#pragma mark Table view delegate methods 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (childController == nil) { 
     childController = [[RecipesListController alloc] initWithStyle:UITableViewStyleGrouped]; 
    } 

    childController.title = @"Ricette"; 
    childController.category = [indexPath row]; 

    [self.navigationController pushViewController:childController animated:YES]; 
} 



@end 

RecipesListController.h CategoryViewController.h

#import <Foundation/Foundation.h> 
#import "RecipeRowViewController.h" 

@class RecipesListController; 

@interface CategoryViewController : UITableViewController { 

    NSArray *recipeCategories; 
    RecipesListController *childController; 
} 

@property (nonatomic, retain) NSArray *recipeCategories; 

@end 

의 코드

#import <Foundation/Foundation.h> 
#import "RecipeRowViewController.h" 

#define kRecipeArrayLink  0 
#define kRecipeArrayDifficulty 1 
#define kRecipeArrayFoodType 2 


#define kRecipeAntipasti 0 
#define kRecipeFocacce  1 
#define kRecipePrimi  2 
#define kRecipeSecondi  3 
#define kRecipeContorni  4 
#define kRecipeDolci  5 


@class DisclosureDetailController; 

@interface RecipesListController : UITableViewController { 

    NSInteger category; 
    NSDictionary *recipesArray; 
    NSArray *recipesNames; 
    NSArray *recipesLinks; 
    DisclosureDetailController *childController; 
} 

@property (nonatomic) NSInteger category; 
@property (nonatomic, retain) NSDictionary *recipesArray; 
@property (nonatomic, retain) NSArray *recipesNames; 
@property (nonatomic, retain) NSArray *recipesLinks; 

@end 

RecipesListcontroller.m

,691,363
#import "RecipesListController.h" 
#import "NavAppDelegate.h" 
#import "DisclosureDetailController.h" 


@implementation RecipesListController 

@synthesize category, recipesArray, recipesNames, recipesLinks; 



- (void)viewDidLoad { 

    // Set background image 
    UIImageView *bgImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sfondo_app.png"]]; 
    [self.tableView setBackgroundView:bgImg]; 
    [bgImg release]; 

    [self.tableView reloadData]; 

    [super viewDidLoad]; 
} 



- (void)viewWillAppear:(BOOL)animated { 

    if (self.recipesArray != nil) { 

     // Release the arrays 
     [self.recipesArray release]; 
     [self.recipesNames release]; 
    } 

    // Load the dictionary 
    NSString *path = nil; 

    // Load a different dictionary, based on the category 
    if (self.category == kRecipeAntipasti) { 
     path = [[NSBundle mainBundle] pathForResource:@"recipes_antipasti" ofType:@"plist"]; 

    } else if (self.category == kRecipeFocacce) { 
     path = [[NSBundle mainBundle] pathForResource:@"recipes_focacce" ofType:@"plist"]; 

    } else if (self.category == kRecipePrimi) { 
     path = [[NSBundle mainBundle] pathForResource:@"recipes_primi" ofType:@"plist"]; 

    } else if (self.category == kRecipeSecondi) { 
     path = [[NSBundle mainBundle] pathForResource:@"recipes_secondi" ofType:@"plist"]; 

    } else if (self.category == kRecipeContorni) { 
     path = [[NSBundle mainBundle] pathForResource:@"recipes_contorni" ofType:@"plist"]; 

    } else if (self.category == kRecipeDolci) { 
     path = [[NSBundle mainBundle] pathForResource:@"recipes_dolci" ofType:@"plist"]; 
    } 

    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    self.recipesArray = dict; 
    [dict release]; 

    // Save recipes names 
    NSArray *array = [[recipesArray allKeys] sortedArrayUsingSelector: 
         @selector(compare:)]; 
    self.recipesNames = array; 

    [self.tableView reloadData]; 
    [super viewWillAppear:animated]; 
} 


- (void)viewDidUnload { 

    self.recipesArray = nil; 
    self.recipesNames = nil; 
    self.recipesLinks = nil; 
// [childController release]; 
    [super viewDidUnload]; 
} 



- (void)dealloc { 

    [recipesArray release]; 
    [recipesNames release]; 
    [recipesLinks release]; 
// [childController release]; 
    [super dealloc]; 
} 




#pragma mark - 
#pragma mark Table data source methods 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [recipesNames count]; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *RecipesListCellId = @"RecipesListCellId"; 

    // Try to reuse a cell or create a new one 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RecipesListCellId]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:RecipesListCellId] autorelease]; 
    } 

    // Get the right value and assign to the cell 
    NSUInteger row = [indexPath row]; 
    NSString *rowString = [recipesNames objectAtIndex:row]; 
    cell.textLabel.text = rowString; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    [rowString release]; 

    return cell; 
} 



#pragma mark - 
#pragma mark Table view delegate methods 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (childController == nil) { 
     childController = [[DisclosureDetailController alloc] initWithNibName:@"DisclosureDetail" bundle:nil]; 
    } 

    childController.title = @"Dettagli"; 
    NSUInteger row = [indexPath row]; 

    childController.recipeName = [recipesNames objectAtIndex:row]; 

    NSArray *recipeRawArray = [recipesArray objectForKey:childController.recipeName]; 
    childController.recipeLink = [recipeRawArray objectAtIndex:kRecipeArrayLink]; 
    childController.recipeDifficulty = [recipeRawArray objectAtIndex:kRecipeArrayDifficulty]; 

    [self.navigationController pushViewController:childController animated:YES]; 
} 



@end 

이 (210)는 충돌 로그입니다

Program received signal: “EXC_BAD_ACCESS”. 
(gdb) bt 
#0 0x00f0da63 in objc_msgSend() 
#1 0x04b27ca0 in ??() 
#2 0x00002665 in -[RecipesListController viewWillAppear:] (self=0x4b38a00, _cmd=0x6d81a2, animated=1 '\001') at /Users/claudiocanino/Documents/iOS/CottoMangiato/Classes/RecipesListController.m:67 
#3 0x00370c9a in -[UINavigationController _startTransition:fromViewController:toViewController:]() 
#4 0x0036b606 in -[UINavigationController _startDeferredTransitionIfNeeded]() 
#5 0x0037283e in -[UINavigationController pushViewController:transition:forceImmediate:]() 
#6 0x04f49549 in -[UINavigationControllerAccessibility(SafeCategory) pushViewController:transition:forceImmediate:]() 
#7 0x0036b4a0 in -[UINavigationController pushViewController:animated:]() 
#8 0x00003919 in -[CategoryViewController tableView:didSelectRowAtIndexPath:] (self=0x4b27ca0, _cmd=0x6d19e3, tableView=0x500c200, indexPath=0x4b2d650) at /Users/claudiocanino/Documents/iOS/CottoMangiato/Classes/CategoryViewCotroller.m:104 
#9 0x0032a794 in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:]() 
#10 0x00320d50 in -[UITableView _userSelectRowAtPendingSelectionIndexPath:]() 
#11 0x000337f6 in __NSFireDelayedPerform() 
#12 0x00d8cfe3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__() 
#13 0x00d8e594 in __CFRunLoopDoTimer() 
#14 0x00ceacc9 in __CFRunLoopRun() 
#15 0x00cea240 in CFRunLoopRunSpecific() 
#16 0x00cea161 in CFRunLoopRunInMode() 
#17 0x016e0268 in GSEventRunModal() 
#18 0x016e032d in GSEventRun() 
#19 0x002c342e in UIApplicationMain() 
#20 0x00001c08 in main (argc=1, argv=0xbfffef58) at /Users/claudiocanino/Documents/iOS/CottoMangiato/main.m:15 

또 다른 BT 로그 :

(gdb) bt 
#0 0x00cd76a1 in __CFBasicHashDeallocate() 
#1 0x00cc2bcb in _CFRelease() 
#2 0x00002dd6 in -[RecipesListController setRecipesArray:] (self=0x6834d50, _cmd=0x4293, _value=0x4e3bc70) at /Users/claudiocanino/Documents/iOS/CottoMangiato/Classes/RecipesListController.m:16 
#3 0x00002665 in -[RecipesListController viewWillAppear:] (self=0x6834d50, _cmd=0x6d81a2, animated=1 '\001') at /Users/claudiocanino/Documents/iOS/CottoMangiato/Classes/RecipesListController.m:67 
#4 0x00370c9a in -[UINavigationController _startTransition:fromViewController:toViewController:]() 
#5 0x0036b606 in -[UINavigationController _startDeferredTransitionIfNeeded]() 
#6 0x0037283e in -[UINavigationController pushViewController:transition:forceImmediate:]() 
#7 0x091ac549 in -[UINavigationControllerAccessibility(SafeCategory) pushViewController:transition:forceImmediate:]() 
#8 0x0036b4a0 in -[UINavigationController pushViewController:animated:]() 
#9 0x00003919 in -[CategoryViewController tableView:didSelectRowAtIndexPath:] (self=0x4b12970, _cmd=0x6d19e3, tableView=0x5014400, indexPath=0x4b2bd00) at /Users/claudiocanino/Documents/iOS/CottoMangiato/Classes/CategoryViewCotroller.m:104 
#10 0x0032a794 in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:]() 
#11 0x00320d50 in -[UITableView _userSelectRowAtPendingSelectionIndexPath:]() 
#12 0x000337f6 in __NSFireDelayedPerform() 
#13 0x00d8cfe3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__() 
#14 0x00d8e594 in __CFRunLoopDoTimer() 
#15 0x00ceacc9 in __CFRunLoopRun() 
#16 0x00cea240 in CFRunLoopRunSpecific() 
#17 0x00cea161 in CFRunLoopRunInMode() 
#18 0x016e0268 in GSEventRunModal() 
#19 0x016e032d in GSEventRun() 
#20 0x002c342e in UIApplicationMain() 
#21 0x00001c08 in main (argc=1, argv=0xbfffef58) at /Users/claudiocanino/Documents/iOS/CottoMangiato/main.m:15 

감사

+0

무엇이 오류입니까? –

+0

여기에 충돌 로그를 게시하여 살펴볼 수 있습니다. –

+0

충돌 로그를 추가했습니다. 앱이 닫힙니다. 나는 더 많은 것을 이해할 수 없다 ... – jollyr0ger

답변

2
// Get the right value and assign to the cell 

NSUInteger row = [dexPath row]in; 
NSString *rowString = [recipeCategories objectAtIndex:row]; 
cell.textLabel.text = rowString; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

[rowString release]; 

나는 매우 비슷한 문제를 가지고 있기 때문에 난 그냥 단서를 찾고 있었다 (초보자 인).하지만 코드를 보면 rowstring을 공개 할 수 있습니까? 당신은 그것을 소유하지 않았습니까? [recipeCategories objectAtIndex:row]은 릴리스에 대해 책임지지 않습니다. create/alloc/copy 메소드가 아닙니다.

관련 문제