我在我的xcode项目中实现了一个SQLite数据库。我想查看从数据库到tableview的所有名称,但收到错误
NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:index 9223372036854775807超出界限[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;
}
答案 0 :(得分:2)
当您搜索某个项目时,如果该方法无法找到该项目,Cocoa将返回NSNotFound
作为索引。这也适用于-indexOfObject:
。 NSNotFound
是一个很大的整数(INT_MAX
),您可以在Q的标题中看到。
因此
NSInteger indexOfname = [self.dbManager.arrColumnNames indexOfObject:@"firstname"];
如果在接收方中找不到NSNotFound
,将为@"firstName"
。请检查您是否有这样的钥匙。错字?骆驼香烟盒? first
N
ams
?
如果您在编译时不知道密钥,则必须检查它,正如@Inder告诉您的那样:
if(indexOfname==NSNotFound)
// The object is not present
顺便说一句:你应该重新检查你的命名。它不遵循Cocoa命名约定。
答案 1 :(得分:0)
请检查indexOfname
或indexOffname
是否等于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
}
答案 2 :(得分:0)
您可以通过非常简单的检查来防止这种情况。
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
}
答案 3 :(得分:0)
实际上,您已成功获取Database
的所有记录。所以左边部分是如何在UITableViewCell
中显示的。根据以下代码修改cellForRowAtIndexPath
。
- (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;
}