0

I am very new to Objective.I am trying to make a simple application and after adding more view I get this error "Thread 1 Signal SIGABRT" and the app wont open in iOS Simulator. The error points to this line of code:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

I tried searching before but I didn't understand what the other answers were talking about.

NamesTableViewController.h is :

#import <UIKit/UIKit.h>


@interface NamesTableViewController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>


@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

@end

NamesTableViewController.m is:

#import "NamesTableViewController.h"

@interface NamesTableViewController ()
@property (nonatomic, copy) NSDictionary *propertyList;
@property (nonatomic, copy) NSArray *letters;
@property (nonatomic, copy)NSMutableArray *filteredNames;
@property (nonatomic, strong)UISearchController *searchController;


@end

    @implementation NamesTableViewController

    @synthesize propertyList,  letters, filteredNames, searchController;

    - (void)viewDidLoad {
        [super viewDidLoad];

        UITableView *tableView = (id)[self.view viewWithTag:1];

        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

        filteredNames = [[NSMutableArray alloc]init];
        searchController = [[UISearchController alloc]init];


        self.searchController.searchResultsUpdater = self;

        NSString *path = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"plist"];
        self.propertyList = [NSDictionary dictionaryWithContentsOfFile:path];
        self.letters = [[self.propertyList allKeys] sortedArrayUsingSelector:@selector(compare:)];
    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        if (tableView.tag == 1){

            return self.letters.count;

        }else {
            return 1;
        }

        }

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

        if (tableView.tag == 1) {

            NSString *letter = self.letters[section];
            NSArray *keyValues = [self.propertyList[letter] allKeys];
            return keyValues.count;
        } else {


            return [filteredNames count];
        }
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...

        if (tableView.tag == 1){

            NSString *letter = self.letters[indexPath.section];;
            NSArray *keyValues = [[self.propertyList[letter] allKeys] sortedArrayUsingSelector:@selector(compare:)];
            cell.textLabel.text = keyValues[indexPath.row];
        } else{
            cell.textLabel.text = filteredNames[indexPath.row];
        }
        return cell;
    }

    -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return self.letters;
    }

    -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

        if (tableView.tag == 1) {
            return letters [section];
        } else {
            return nil;
        }
    }

    #pragma mark Search Display Delegate Methods

    -(void)searchDisplayController:(UISearchController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    }



    -(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString

    {

        [filteredNames removeAllObjects];
        if (searchString.length > 0) {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text];

            for (NSString *letter in letters) {
                NSArray *matches = [[self.propertyList[letter] allKeys]filteredArrayUsingPredicate:predicate];

                [filteredNames addObjectsFromArray:matches];

            }

        }

        return YES;
    }

    @end

If you want more information just say it to me by answers and I will edit my question and then you will edit your answer

Fa Hu
  • 15
  • 8
  • Did you activate the exception breakpoints and saw where it crashes? (if you don't know how to, check this link http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode ) – Ulysses Feb 12 '16 at 16:44
  • See http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 – rmaddy Feb 12 '16 at 16:45
  • İ did it `- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *key = names[section]; NSArray *keyValues = keys[key]; return [keyValues count]; }` İt is working well – Fa Hu Feb 13 '16 at 07:04

1 Answers1

0

[keys allKeys] returns an single-level array. Therefore there can be only one section.

Edit:

For better understanding and readability I replaced names with letters and keys with propertyList

• .h

The methods numberOfRowsInSection and cellForRowAtIndexPath retrieve the section letter and then the appropriate keys from the property list.

A dictionary as data source is not the best choice because there are many repetitive tasks to perform for example the sorting. Better create an nested array as data source in viewDidLoad

@interface NamesTableViewController ()
@property (nonatomic, copy) NSDictionary *propertyList;
@property (nonatomic, copy) NSArray *letters;

@end

• .m

@implementation NamesTableViewController

@synthesize propertyList, letters;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"plist"];
    self.propertyList = [NSDictionary dictionaryWithContentsOfFile:path];
    self.letters = [[self.propertyList allKeys] sortedArrayUsingSelector:@selector(compare:)];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.letters.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *letter = self.letters[section];
    NSArray *keyValues = [self.propertyList[letter] allKeys];
    return keyValues.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    NSString *letter = self.letters[indexPath.section];
    NSArray *keyValues = [[self.propertyList[letter] allKeys] sortedArrayUsingSelector:@selector(compare:)];
    cell.textLabel.text = keyValues[indexPath.row];
    return cell;
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.letters;
}

@end
vadian
  • 274,689
  • 30
  • 353
  • 361