Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7166147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:21:59+00:00 2026-05-28T14:21:59+00:00

I think just showing my whole code is better than my explanation.. actually I’m

  • 0

I think just showing my whole code is better than my explanation.. actually I’m not good at expressing something in English.X(

this code is .h file for .m file below.

#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

this code is for the .m file that implement showing a table. When I try to scroll the table down, the error named EXC_BAD_ACCESS is suddenly called at – (NSString *)tableView: titleForHeaderInSection: method. I guess sortedKeys releases at somewhere because I can do [sortedKeys count] before error, but cannot when error comes. I don’t know where it releases and why it releases, however. Please show your ideas to me. Any ideas are okay. Thank you in advance.

#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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:22:00+00:00Added an answer on May 28, 2026 at 2:22 pm

    sortedKeys is not properly retained. When you set it use self.

    self.sortedKeys = [self.keys sortedArrayUsingSelector:@selector(sortForIndex:)];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not very used to programming with flags, but I think I just found
I think I'm just missing something simple here but here is what I am
just think that when I opened my file then when I want to write
I think this is just a Best Practices question, but I was wondering if
[see later answer for more] I think this is just a simple rails question,
Okay, I think I'm just making a stupid mistake here, but I want to
I have read much information about agile and waterfall and I just cannot think
Just when I think I'm getting an understanding of the syntax of Javascript, I
So I just got my site kicked off the server today and I think
I think the solution is really simple, I just haven't come across it online.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.