Reputation: 41
I have an SQLite database implemented in my xcode project. i want to view all name from database into tableview but getting an error as
NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 13]'
@interface DisplayViewController ()
@property (nonatomic, strong) DBManager *dbManager;
@property (nonatomic, strong) NSArray *arrPeopleInfo;
@end
@implementation DisplayViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.mytableview.delegate = self;
self.mytableview.dataSource = self;
NSString *docsDir;
NSArray *dirPaths;
//Get the directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"IApp.db"]; //Build the path to keep the database
// _databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"IApp.db"]];
// [self getcount];
[self loadData];
// [self getcount];
// Do any additional setup after loading the view.
}
- (void)loadData {
// Form the query.
NSString *query = @"select * from iApp";
if (self.arrPeopleInfo != nil) {
self.arrPeopleInfo = nil;
}
self.arrPeopleInfo = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
[self.mytableview reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arrPeopleInfo.count;
// return [arri count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idCellRecord" forIndexPath:indexPath];
NSInteger indexOfname = [self.dbManager.arrColumnNames indexOfObject:@"firstname"];
NSInteger indexOffname = [self.dbManager.arrColumnNames indexOfObject:@"lastname"];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfname], [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOffname]];
return cell;
}
Upvotes: 2
Views: 2438
Reputation: 3733
Actually you have successfully fetched all record from Database
. so left part is how to shown in UITableViewCell
. modified cellForRowAtIndexPath
as per below code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idCellRecord" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [[self.arrPeopleInfo objectAtIndex:indexPath.row] valeuForKey:@"firstname"], [[self.arrPeopleInfo objectAtIndex:indexPath.row] valueForKey:@"lastname"]];
return cell;
}
Upvotes: 0
Reputation: 39978
Please check whether indexOfname
or indexOffname
is equal to NSNotFound
NSInteger indexOfname = [self.dbManager.arrColumnNames indexOfObject:@"firstname"];
NSInteger indexOffname = [self.dbManager.arrColumnNames indexOfObject:@"lastname"];
if (indexOfname == NSNotFound) {
//Then don't use this index
}
if (indexOffname == NSNotFound) {
//Then don't use this index
}
Upvotes: 0
Reputation: 7207
You can prevent this by using a very simple checking.
if(indexOfname < [self.dbManager.arrColumnNames count] && indexOffname < [self.dbManager.arrColumnNames count]) {
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfname], [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOffname]];
}
else {
cell.textLabel.text = @""; // or what ever you want to do
}
Upvotes: 0
Reputation: 16650
When you search for an item in something, Cocoa returns NSNotFound
for the index, if the method cannot find the item. This applies for -indexOfObject:
, too. NSNotFound
is a big integral number (INT_MAX
) as you can see in the title of your Q.
Therefore
NSInteger indexOfname = [self.dbManager.arrColumnNames indexOfObject:@"firstname"];
will be NSNotFound
, if @"firstName"
is not found in the receiver. Please check, whether you have such a key. Typo? Camel case? first
N
ams
?
If you do not know the keys at compile time you have to check for it, as @Inder told you:
if(indexOfname==NSNotFound)
// The object is not present
BTW: You should really recheck your naming. It does not follow the Cocoa naming conventions.
Upvotes: 2