2012-01-26 17 views
0

私はこのコードがある突然EXC_BAD_ACCESS - (NSStringの*)のtableView:titleForHeaderInSection:

(実際に私はEnglish.Xで何かを表現するのが苦手...ちょうど私のコード全体を示したことは私の説明よりも優れていると思います以下の.mファイルの.hファイル

#import <UIKit/UIKit.h> 

#define kImageValueTag  0 
#define kNameValueTag  1 
#define kSubtitleValueTag 2 
#define kMemoValueTag  3 

@class PhonebookDetailedViewController; 

@interface PhonebookViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
{ 
    UITableViewCell *tvCell; 
    UITableView *tableView; 
    UISearchBar *searchBar; 

    NSString   *fullName; 

    NSMutableDictionary  *names, *pictures; 
    NSArray    *keys, *sortedKeys, *sortedAllValues; 

    PhonebookDetailedViewController *childController; 
} 

@property (nonatomic, retain) IBOutlet UITableViewCell *tvCell; 
@property (nonatomic, retain) IBOutlet UISearchBar *searchBar; 
@property (nonatomic, retain) IBOutlet UITableView *tableView; 
@property (nonatomic, retain) UIImage  *phoneImage; 
@property (nonatomic, retain) NSString  *fullName; 

@property (nonatomic, retain) NSMutableDictionary *names; 
@property (nonatomic, retain) NSMutableDictionary *pictures; 
@property (nonatomic, retain) NSArray  *keys; 
@property (nonatomic, retain) NSArray  *sortedKeys; 
@property (nonatomic, retain) NSArray  *sortedAllValues; 

@end 

このコード表を示す実装.Mファイルのためである私がダウンして、テーブルをスクロールしようとすると、EXC_BAD_ACCESSという名前のエラーが突然で呼ばれて - 。。(NSStringの*)tableView:titleForHeaderInSection:method。私は[sortedKeys count]をエラーの前に行うことができるので、sortedKeysがどこかで解放すると思うが、エラーが来るとs。私はそれがどこでリリースされ、なぜそれがリリースされるのかはわかりません。あなたのアイデアを私に見せてください。すべてのアイデアは大丈夫​​です。前もって感謝します。

#import "PhonebookViewController.h" 
#import "PhonebookDetailedViewController.h" 
#import "NSString-SortForIndex.h" 

@implementation PhonebookViewController 

@synthesize tvCell; 
@synthesize searchBar; 
@synthesize tableView; 
@synthesize phoneImage; 
@synthesize fullName; 

@synthesize names, pictures; 
@synthesize keys, sortedAllValues, sortedKeys; 

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
//{ 
// self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
// if (self) { 
//  // Custom initialization 
// } 
// return self; 
//} 

//- (void)didReceiveMemoryWarning 
//{ 
// // Releases the view if it doesn't have a superview. 
// [super didReceiveMemoryWarning]; 
//  
// // Release any cached data, images, etc that aren't in use. 
//} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    CGRect bounds = self.tableView.bounds; 
    bounds.origin.y = bounds.origin.y + searchBar.bounds.size.height; 
    self.tableView.bounds = bounds; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *nameFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"nameContacts.dict"]; 
    NSString *pictureFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"imageContacts.dict"]; 

    self.names = (NSMutableDictionary *)[NSKeyedUnarchiver unarchiveObjectWithFile:nameFilePath]; 
    self.pictures = (NSMutableDictionary *)[NSKeyedUnarchiver unarchiveObjectWithFile:pictureFilePath]; 
    self.keys = [self.names allKeys]; 

    sortedKeys = [self.keys sortedArrayUsingSelector:@selector(sortForIndex:)]; 
    sortedAllValues = [[NSArray alloc] init]; 
    for (NSString *sortedKey in sortedKeys) 
    { 
     NSArray *selectedValues = [self.names valueForKey:sortedKey]; 
     for (NSString *selectedValue in selectedValues) 
      sortedAllValues = [sortedAllValues arrayByAddingObject:selectedValue]; 
    } 

    // Do any additional setup after loading the view from its nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 

    self.tableView = nil; 
    self.searchBar = nil; 
    self.tvCell  = nil; 
    self.fullName  = nil; 

    self.names  = nil; 
    self.pictures = nil; 
    self.keys  = nil; 
    self.sortedKeys = nil; 
    self.sortedAllValues = nil; 

} 

- (void)dealloc 
{ 
    [tableView release]; 
    [searchBar release]; 
    [tvCell release]; 
    [fullName  release]; 

    [names release]; 
    [pictures release]; 
    [keys release]; 
    [sortedKeys release]; 
    [sortedAllValues release]; 

    [super dealloc]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table Data Source Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{  
    return [sortedKeys count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSString *key = [sortedKeys objectAtIndex:section]; 
    NSArray  *nameSection = [names objectForKey:key]; 
    return [nameSection count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 

    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] 
          loadNibNamed:@"CustomPhonebookCell" 
          owner:self 
          options:nil]; 
     if (nib.count > 0) 
     { 
      cell = self.tvCell; 
     } else 
     { 
      NSLog(@"Failed to load CustomPhonebookCell nib file!"); 
     } 
    } 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
    NSUInteger row = [indexPath row]; 
    NSUInteger section = [indexPath section]; 

    NSString *foundKey = [sortedKeys objectAtIndex:section]; 
    NSArray *nameSection = [self.names objectForKey:foundKey]; 

    UILabel *nameLabel = (UILabel *)[cell viewWithTag:kNameValueTag]; 
    nameLabel.text = [nameSection objectAtIndex:row];   

    return cell; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSString *key = [sortedKeys objectAtIndex:section]; // EXC_BAD_ACCESS error at here 
    return key; 
} 

答えて

3

sortedKeysは正しく保持されません。あなたがそれを使用するときに設定するself.

self.sortedKeys = [self.keys sortedArrayUsingSelector:@selector(sortForIndex:)]; 
+0

なぜ私はそれをチェックしなかったのですか?どうもありがとうございます!! –