2015-04-17 84 views
2

我在我的xcode項目中實現了一個SQLite數據庫。我想從數據庫中查看所有的名字進入的tableview,但得到一個錯誤[__NSArrayM objectAtIndex:]:index 9223372036854775807 beyond bounds [0 .. 13]'

NSRangeException」,原因: '*** - [__ NSArrayM objectAtIndex:]:指數 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

請檢查indexOfnameindexOffname是否等於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 
} 
+0

如何檢查nsfound?你能告訴我代碼嗎? –

2

當您搜索某物品時,Cocoa會返回NSNotFound作爲索引,如果該方法找不到該物品。這也適用於-indexOfObject:NSNotFound是一個大整數(INT_MAX),你可以在你的Q.

的標題看到的

因此

NSInteger indexOfname = [self.dbManager.arrColumnNames indexOfObject:@"firstname"]; 

NSNotFound,如果@"firstName"沒有在接收器中。請檢查,你是否有這樣的鑰匙。錯字?駱駝香菸盒? firstNams

如果你不知道在編譯時,你必須檢查它的鑰匙,因爲@Inder告訴你:

if(indexOfname==NSNotFound) 
    // The object is not present 

BTW:你真的應該重新檢查你的命名。它不遵循可可命名約定。

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 
} 
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; 

} 
+0

評論不適合擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/75528/discussion-on-answer-by-none-nsarraym-objectatindex-index-9223372036854775)。 – Taryn

相關問題