Code is bypassing Array creation Loop from a sqlite database - objective-c

my NSMutableArray creation code is being bypassed altogether for some reason. in theory it is supposed to create an NSMutableArray based on an sqlite database. There is only one warning message and no errors. what am I missing?
the implementation file is:
#import "iProspectFresno LiteAppDelegate.h"
#import "MainViewController.h"
#import "Mine.h"
#import <Foundation/Foundation.h>
#implementation iProspectFresno_LiteAppDelegate
#synthesize window;
#synthesize mainViewController;
#synthesize mines;
-(void) checkAndCreateDatabase {
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
-(void) readMinesFromDatabase
{
sqlite3 *database;
mines = [[NSMutableArray alloc] init];
NSLog(#"readMinesFromDatabase initialized");
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select * from MinesoftheMotherLode";
sqlite3_stmt *compiledStatement;
NSLog(#"first if statement");
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
NSLog(#" second if statement initialized");
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSNumber *aentryNumber = [NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 1)];
NSString *amineName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 2)];
NSString *amineType = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 3)];
NSString *astatus = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 4)];
NSNumber *alatitude = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 5)];
NSNumber *alongitude = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 6)];
NSString *ametal =[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 7)];
BOOL *adisplay = NO;
NSNumber *acoverRegion =[NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 9)];
NSLog(#"mine", aentryNumber, amineName, amineType, astatus, alatitude, alongitude, ametal, adisplay, acoverRegion);
Mine *mine = [[Mine alloc] initWithEntryNumber:aentryNumber mineName:amineName mineType:amineType status:astatus latitudeInitial:alatitude longitudeInitial:alongitude metal:ametal display:adisplay coverRegion:acoverRegion];
[mines addobject:mine];
[mine release];
}
}
NSLog(#"created database successfully");
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
databaseName = #"MinesoftheMotherLode.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
[self readMinesFromDatabase];
MainViewController *aController = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window addSubview:[mainViewController view]];
[window makeKeyAndVisible];
}
The implementation file for Mines is here:
#import "Mine.h"
#implementation Mine
#synthesize entryNumber, mineName, mineType, status, latitudeInitial, longitudeInitial, metal, display, coverRegion;
-(id)initWithEntryNumber:(NSNumber *)e mineName:(NSString *)n mineType:(NSString *)t status:(NSString *)s latitudeInitial:(NSNumber *)l longitudeInitial:(NSNumber *)o metal:(NSString *)m display:(BOOL *)d coverRegion:(NSNumber *)c
{
self.entryNumber = e;
self.mineName = n;
self.mineType = t;
self.status = s;
self.latitudeInitial = l;
self.longitudeInitial = o;
self.metal = m;
self.display = d;
self.coverRegion = c;
return self;
}
#end
The NSLog "Second if statement initialized" is not showing up on the console. any ideas as to what needs to be fixed here? and yes I know, I should be using core data.

It seems you've answered your initial question about it loading. For your secondary question regarding the crash about the null string, you should be loading strings like this:
if (sqlite3_column_text(init_statement, 0) != NULL) {
self.someString = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 0)];
} else {
self.someString = #"";
}

Related

run applescript from cocoa app stopped working

This code had been working fine until just recently. I hadn't' changed anything nor upgraded my system and I'm completely flummoxed.
I've been using it for 6 years and now it dies on me.
Is there an easier or better way of running an applescript from within a cocoa application? At this point I'm happy to pay to fix this problem!
utils.h
#import <Foundation/Foundation.h>
#interface Utils : NSObject
// Runs an applescript with a given map of variables (name/value)
+ (NSArray *)runApplescript:(NSString *)source withVariables:(NSDictionary *)variables;
// Runs an applescript from a file pathwith a given map of variables
// (name/value)
+ (NSArray *)runApplescriptFromFile:(NSString *)scriptName withVariables:(NSDictionary *)variables;
+ (NSArray *)arrayFromDescriptor:(NSAppleEventDescriptor *)descriptor;
// String is empty or only has white characters (space, tab...)
+ (BOOL)stringIsEmptyOrWhite:(NSString *)string;
#end
Utils.M
#import "Utils.h"
#implementation Utils
+ (NSArray *)arrayFromDescriptor:(NSAppleEventDescriptor *)descriptor {
// Enumerate the apple descriptors (lists) returned by the applescript and
// make them into arrays
NSMutableArray *returnArray = [NSMutableArray array];
NSInteger counter, count = [descriptor numberOfItems];
for (counter = 1; counter <= count; counter++) {
NSAppleEventDescriptor *desc = [descriptor descriptorAtIndex:counter];
if (nil != [desc descriptorAtIndex:1]) {
[returnArray addObject:[Utils arrayFromDescriptor:desc]];
} else {
NSString *stringValue = [[descriptor descriptorAtIndex:counter] stringValue];
if (nil != stringValue) {
[returnArray addObject:stringValue];
} else {
[returnArray addObject:#""];
}
}
}
return returnArray;
}
+ (NSString *)escapeCharacters:(NSString *)string {
return [string stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
}
+ (NSArray *)runApplescript:(NSString *)source withVariables:(NSDictionary *)variables {
NSString *input = #"";
NSArray *variableNames = [variables allKeys];
// Transform the dictionary of names/values to set sentences of applescript
for (NSString *variableName in variableNames) {
NSObject *variableValue = [variables objectForKey:variableName];
if ([variableValue isKindOfClass:[NSString class]]) {
input =
[input stringByAppendingString:[NSString stringWithFormat:#"set %# to (\"%#\" as text)\n", variableName,
[Utils escapeCharacters:variableValue], nil]];
} else if ([variableValue isKindOfClass:[NSNumber class]]) {
input = [input stringByAppendingString:[NSString stringWithFormat:#"set %# to (%# as integer)\n",
variableName, variableValue, nil]];
} else if ([variableValue isKindOfClass:[NSArray class]]) {
// Initialize a list
NSString *entry;
NSArray *values = (NSArray *)variableValue;
input = [input stringByAppendingString:[NSString stringWithFormat:#"set %# to {", variableName]];
BOOL first = TRUE;
for (entry in values) {
if (!first) {
input = [input stringByAppendingString:#", "];
}
input = [input
stringByAppendingString:[NSString stringWithFormat:#"\"%#\"", [Utils escapeCharacters:entry], nil]];
first = FALSE;
}
input = [input stringByAppendingString:#"}\n"];
}
}
NSString *finalScript = [input stringByAppendingString:[NSString stringWithFormat:#"\n\n%#", source]];
NSLog(#"Final script: %#", finalScript);
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:finalScript];
NSDictionary *error;
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&error];
NSLog(#"applescript error: %#", [error description]);
// Transform the return value of applescript to nested nsarrays
return [Utils arrayFromDescriptor:descriptor];
}
+ (NSArray *)runApplescriptFromFile:(NSString *)scriptName withVariables:(NSDictionary *)variables {
NSString *scriptPath = [[NSBundle mainBundle] pathForResource:scriptName ofType:#"applescript"];
NSString *scriptSource =
[[NSString alloc] initWithContentsOfFile:scriptPath encoding:NSASCIIStringEncoding error:nil];
return [Utils runApplescript:scriptSource withVariables:variables];
}
+ (BOOL)stringIsEmptyOrWhite:(NSString *)string {
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
return [string isEqualToString:#""];
}
#end
Easier, yes, although whether that’s your actual problem is another question.
http://appscript.sourceforge.net/asoc.html
I assume you’ve already got other details, including sandboxing and hardening settings and plist entries, taken care of. (Recent Xcode upgrades also had a habit of breaking it when auto-upgrading your project files, by turning on hardening for you so Apple events can’t get out.)

SQLite Problems when using NSFileManager

I am using SQLite and previous answers state that I should use this code:
#import "ViewController.h"
#interface ViewController ()
{
NSMutableArray *arrayOfPerson;
sqlite3 *personDB;
NSString *dbPathString;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
arrayOfPerson = [[NSMutableArray alloc]init];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
[self createOrOpenDB];
}
- (void)createOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:#"persons.db"];
char *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:dbPathString];
NSLog(#"here 0");
if (!success) {// THIS IS MY CONCERN
const char *dbPath = [dbPathString UTF8String];
NSLog(#"here 1");
//creat db here
NSLog(#"sqlite3 = %d",sqlite3_open(dbPath, &personDB));
NSLog(#"OK= %d", SQLITE_OK);
if (sqlite3_open(dbPath, &personDB)==SQLITE_OK) {
NSLog(#"here 2");
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS PERSONS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, AGE INTEGER)";
sqlite3_exec(personDB, sql_stmt, NULL, NULL, &error);
sqlite3_close(personDB);
}
}
}
However, when I run this code my app does not gets into this loop:
if(!success) {
....
}
I tried using
if(success) {
....
}
and it works. But I do not think that is correct. Please help me.

Sqlite create database

I'm new to sqlite, and i've been for a couple of days trying to follow a tutorial, but it do not want to add my database and table. It gives me the alert that i've made if the table is not added. "the TABLE has not been created"
i do not get any xcode errors, so it could be something else than this code i guess. anybody have any suggestions?
viewcontroller.h
#property (retain, nonatomic) NSString *databaseName, *tableName;
#property (readwrite, nonatomic) int numberOfRows;
#property (readwrite, nonatomic) NSMutableArray *dataList;
#property (readwrite, nonatomic) BOOL table_ok, db_open_status;
#property (retain, nonatomic) NSArray *my_columns_names;
viewcontroller.h
[super viewDidLoad];
dataList = [[NSMutableArray alloc]init];
numberOfRows = 0;
databaseName = #"mysampledatabase";
tableName = #"mypeople";
db_open_status = NO;
table_ok = NO;
my_columns_names = [[NSArray alloc]initWithObjects:#"hometeam",#"awayteam",#"homeplayers",#"homefouls",#"awayplayers",#"awayfouls",#"period",#"time",#"homescore",#"awayscore", nil];
if ([self openDBWithSQLName:databaseName]) {
db_open_status = YES;
if (![self createTable:tableName WithCoulumns:my_columns_names]) {
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Warning" message:#"the TABLE has not been created" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[av show];
} else {
table_ok = YES;
}
}
-(BOOL)openDBWithSQLName:(NSString *)sqlname{
BOOL is_Opened = NO;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *my_sqlfile = [[paths objectAtIndex:0] stringByAppendingPathComponent:sqlname];
if (sqlite3_open([my_sqlfile UTF8String], &dbname) ==SQLITE_OK) {
is_Opened = YES;
}
return is_Opened;
}
-(BOOL)createTable:(NSString *)tablename WithCoulumns:(NSArray *)columnNames{
BOOL has_beencreated = NO;
NSString *fieldset = #"";
char *err;
for (int a=0; a<[columnNames count]; a++){
NSString *columnSet = [NSString stringWithFormat:#"'%#' TEXT", [columnNames objectAtIndex:a]];
fieldset = [fieldset stringByAppendingString:columnSet];
if (a<([columnNames count]-1)) {
fieldset = [fieldset stringByAppendingString:#" ,"];
}
}
NSString *sql = [NSString stringWithFormat:#"CREATE TABLE IF NOT EXISTS '%#' (ID INTEGER PRIMARY KEY AUTOINCREMENT,%#", tableName, fieldset];
if (sqlite3_exec(dbname, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(dbname);
} else {
has_beencreated = YES;
}
return has_beencreated;
}
I think I figured out the issue with the "the TABLE has not been created" message you receive (I did, as well!). In the createTable: method just before the assignment of *sql if you add this code:
fieldset=[fieldset stringByAppendingString:#")"];
the fiedlset is fulfilled with a closing bracket that it misses.
You can check it yourself by putting a breakpoint a little below this at:
has_beencreated = YES;
run the app and check the variable &err
Hope it helps!

"Can't allocate region" malloc error when running millions of iterations of loop

Thanks to a lot of help I've received here on SO, I've gotten an algorithm to check a list of around 15,000 8-letter words for any partial anagrams, against a list of around 50,000 total words (so I suppose a total of 108 million iterations). I call this method once for each comparison (so 750 million times). I'm getting the following error, always somewhere in the midst of the 119th iteration through the 1,350 there should be:
AnagramFINAL(2960,0xac8c7a28) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
I've narrowed the memory issue down to being a huge number of allocated CFStrings (immutable). Any idea what I can do to remedy the issue? I'm using ARC and an #autoreleasepool, not sure what else I could do, it seems something isn't being released when it should be.
AnagramDetector.h
#import <Foundation/Foundation.h>
#interface AnagramDetector : NSObject {
NSDictionary *allEightLetterWords;
NSDictionary *allWords;
NSFileManager *fileManager;
NSArray *paths;
NSString *documentsDirectory;
NSString *filePath;
}
- (BOOL) does: (NSString *) longWord contain: (NSString *) shortWord;
- (NSDictionary *) setupAllWordList;
- (NSDictionary *) setupEightLetterWordList;
- (void) saveDictionary: (NSMutableDictionary *)currentArray;
#end
AnagramDetector.m
#implementation AnagramDetector
- (id) init {
self = [super init];
if (self) {
fileManager = [NSFileManager defaultManager];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
}
return self;
}
- (BOOL) does: (NSString *) longWord contain: (NSString *) shortWord {
#autoreleasepool {
NSMutableString *longerWord = [longWord mutableCopy];
for (int i = 0; i < [shortWord length]; i++) {
NSString *letter = [shortWord substringWithRange: NSMakeRange(i, 1)];
NSRange letterRange = [longerWord rangeOfString: letter];
if (letterRange.location != NSNotFound) {
[longerWord deleteCharactersInRange: letterRange];
} else {
return NO;
}
}
return YES;
}
}
- (NSDictionary *) setupAllWordList {
#autoreleasepool {
NSString *fileWithAllWords = [[NSBundle mainBundle] pathForResource:#"AllDefinedWords" ofType:#"plist"];
allWords = [[NSDictionary alloc] initWithContentsOfFile: fileWithAllWords];
NSLog(#"Total number of words: %d.", [allWords count]);
}
return allWords;
}
- (NSDictionary *) setupEightLetterWordList {
#autoreleasepool {
NSString *fileWithEightWords = [[NSBundle mainBundle] pathForResource:#"AllDefinedEights" ofType:#"plist"];
allEightLetterWords = [[NSDictionary alloc] initWithContentsOfFile: fileWithEightWords];
NSLog(#"Total number of words: %d.", [allEightLetterWords count]);
}
return allEightLetterWords;
}
- (void) saveDictionary: (NSMutableDictionary *)currentArray {
#autoreleasepool {
filePath = [documentsDirectory stringByAppendingPathComponent: #"A.plist"];
[fileManager createFileAtPath:filePath contents: nil attributes: nil];
[currentArray writeToFile: filePath atomically:YES];
[currentArray removeAllObjects];
}
}
#end
Code running on launch (inside AppDelegate for now, since no VC):
#autoreleasepool {
AnagramDetector *detector = [[AnagramDetector alloc] init];
NSDictionary *allWords = [[NSDictionary alloc] initWithDictionary:[detector setupAllWordList]];
NSDictionary *eightWords = [[NSDictionary alloc] initWithDictionary:[detector setupEightLetterWordList]];
int remaining = [eightWords count];
for (NSString *currentEightWord in eightWords) {
if (remaining % 10 == 0) NSLog(#"%d ::: REMAINING :::", remaining);
for (NSString *currentAllWord in allWords) {
if ([detector does: [eightWords objectForKey: currentEightWord] contain: [allWords objectForKey: currentAllWord]]) {
// NSLog(#"%# ::: CONTAINS ::: %#", [eightWords objectForKey: currentEightWord], [allWords objectForKey: currentAllWord]);
}
}
remaining--;
}
}
The problem seems to be that a lot of autoreleased objects fill up the memory waiting to be released. So a solution is to add your own autorelease pool scope to collect autoreleased objects and release them sooner.
I suggest that you do something like this:
for (NSString *currentEightLetterWord in [eightLetterWordsDictionary allKeys]) {
#autoreleasepool {
for (NSString *currentWord in [allWordsDictionary allKeys]) {
}
}
}
Now all autoreleased objects inside #autoreleasepool { .. } will be released for each iteration of the outer loop.
As you see ARC might save you from thinking about most reference counting and memory management issues but objects can still end up in autorelease pools with ARC when using methods that directly or indirectly create autoreleased objects.
An alternative solution that I don't really recommend is to try to avoid using method that will use autorelease. Then does:contain: could awkwardly be rewritten to something like this:
- (BOOL) does: (NSString* ) longWord contain: (NSString *) shortWord {
NSMutableString *haystack = [longWord mutableCopy];
NSMutableString *needle = [shortWord mutableCopy];
while([haystack length] > 0 && [needle length] > 0) {
NSMutableCharacterSet *set = [[NSMutableCharacterSet alloc] init];
[set addCharactersInRange:NSMakeRange([needle characterAtIndex:0], 1)];
if ([haystack rangeOfCharacterFromSet:set].location == NSNotFound) return NO;
haystack = [haystack mutableCopy];
[haystack deleteCharactersInRange:NSMakeRange(0, [haystack rangeOfCharacterFromSet: set].location)];
needle = [needle mutableCopy];
[needle deleteCharactersInRange:NSMakeRange(0, 1)];
}
return YES;
}

How to implement sort functionality same as AddressBook?

In my app, I have list of contacts which are displayed in ascending order.When user clicks on any alphabet say 'b' then the list should scrolls to the contact starting from 'b'.Is this built-In functionality of AddressBook?Can anyone knows how I can achieve this?
Thanks in advance!
My pretty dirty method. It sorts by email, first name and last name omitting middle name cause I didn't needed that one. Oh and it finds only those contacts which have email address. You can avoid that if you slightly edit code starting with if (ABMultiValueGetCount(emailRef))
Your view controller:
- (NSArray *)sortedContactsFromPeople:(CFArrayRef)people {
NSMutableArray *contacts = [NSMutableArray array];
for (int i = 0; i < CFArrayGetCount(people); i++) {
ABRecordRef record = CFArrayGetValueAtIndex(people, i);
ABMultiValueRef emailRef = ABRecordCopyValue(record, kABPersonEmailProperty);
CFStringRef email;
if (ABMultiValueGetCount(emailRef)) {
BOOL hasValidEmail = NO;
for (int j = 0; j < ABMultiValueGetCount(emailRef); j++) {
if (!hasValidEmail) {
email = ABMultiValueCopyValueAtIndex(emailRef, j);
if ([Validator validateEmail:(NSString *)email] == kValNoErr)
hasValidEmail = YES;
else
CFRelease(email);
}
}
if (hasValidEmail) {
CFStringRef name = ABRecordCopyValue(record, kABPersonFirstNameProperty);
CFStringRef lastname = ABRecordCopyValue(record, kABPersonLastNameProperty);
NSData *contactImageData = (NSData*)ABPersonCopyImageData(record);
UIImage *img = [[[UIImage alloc] initWithData:contactImageData] autorelease];
[contactImageData release];
if (lastname == nil)
lastname = (CFStringRef)#"";
if (name == nil)
name = (CFStringRef)#"";
Contact *contact = [[[Contact alloc] initWithName:(NSString *)name
lastname:(NSString *)lastname
email:(NSString *)email
profileIcon:img] autorelease];
if (![(NSString *)lastname isEqualToString:#""])
contact.sortChar = [(NSString *)lastname substringToIndex:1];
else if (![(NSString *)name isEqualToString:#""])
contact.sortChar = [(NSString *)name substringToIndex:1];
else if (![(NSString *)email isEqualToString:#""])
contact.sortChar = [(NSString *)email substringToIndex:1];
contact.idNumber = ABRecordGetRecordID(record);
[contacts addObject:contact];
if (lastname)
CFRelease(lastname);
if (name)
CFRelease(name);
CFRelease(email);
}
}
CFRelease(emailRef);
}
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:#"sortChar" ascending:YES selector:#selector(caseInsensitiveCompare:)];
[contacts sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
return contacts;
}
- (void)initBaseValues {
sections = [[NSMutableDictionary alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSInteger section = 0;
NSString *prevChar = nil;
NSArray *contacts = [self sortedContactsFromPeople:people];
for (int i = 0; i < contacts.count; i++) {
Contact *contact = [contacts objectAtIndex:i];
BOOL sectionExists = NO;
if ([prevChar isEqualToString:contact.sortChar])
sectionExists = YES;
if (!sectionExists) {
[sections setObject:[NSMutableArray array] forKey:[NSString stringWithFormat:#"%d", section]];
section++;
}
[prevChar autorelease];
prevChar = [contact.sortChar copy];
[[sections objectForKey:[NSString stringWithFormat:#"%d", section-1]] addObject:contact];
}
if (prevChar != nil)
[prevChar release];
CFRelease(people);
CFRelease(addressBook);
}
Contact.h
#interface Contact : NSObject {
NSString *name;
NSString *lastname;
NSString *email;
UIImage *profileIcon;
NSInteger idNumber;
}
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *lastname;
#property (nonatomic, copy) NSString *email;
#property (nonatomic, retain) UIImage *profileIcon;
#property (nonatomic) NSInteger idNumber;
#property (nonatomic, copy) NSString *sortChar;
- (id)initWithName:(NSString *)name_
lastname:(NSString *)lastname_
email:(NSString *)email_
profileIcon:(UIImage *)profileIcon_;
#end
Doh! I wasn't vigilant enough, to read the whole thing carefully. :) Try creating NSMutableDictionary and each time headerForSection: method is being called store it's offset in the dictionary with appropriate letter as key. Then when user selects "B" letter send your UITableView setContentOffset:animated: method with appropriate offset taken from that dictionary.