-1

我是 iOS 开发的新手,我遇到了奇怪的问题(因为表视图控制器和 sql 类中的相同代码与其他实体一起工作得很好)。

我正在尝试将数据从 sqlite 数据库检索到我的表视图控制器..

当我尝试在数据库中插入多条记录时,会出现此错误。 2012-11-10 19:04:20.577 我的应用程序 [7032:11303] * 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* - [__NSArrayI objectAtIndex:]:索引 1 超出范围 [0 .. 0]” *首先抛出调用栈:

任何帮助将不胜感激。 谢谢你。


import <UIKit/UIKit.h>
import "Category.h"
import "DbOperations.h"
import "AddCategoriesTVC.h"
import "CategoryDetailTVC.h"@interface CategoriesTVC : UITableViewController <UITableViewDataSource, UITableViewDelegate,AddCategoriesTVCDelegate, CategoryDetailTVCDelegate>

@property (strong, nonatomic) Category *categoryItem;
@property(nonatomic, strong) DbOperations * ops;
@property(nonatomic, strong) NSMutableArray * myCategoryArray;
@property (strong, nonatomic) IBOutlet UITableView * categoryTable;
@end
///////////
import "CategoriesTVC.h"
import "Category.h"
import "DbOperations.h"
import "sqlite3.h"
@implementation CategoriesTVC
@synthesize categoryTable;
@synthesize categoryItem,myCategoryArray,ops;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{ops = [[DbOperations alloc]init];
[ops selectCategories];
myCategoryArray = [ops categoryArray];
self.editButtonItem.title = @"edit";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[super viewDidLoad];
}


- (void)viewDidUnload
{
[self setCategoryTable:nil];
[super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{[super viewWillAppear:animated];
//myCategoryArray =[[NSMutableArray alloc]init];//]
[ops selectCategories];
myCategoryArray = [ops categoryArray];
[categoryTable reloadData];}

//pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// Return the number of sections.
return 1;}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 NSLog(@"JJJJ2");
// Return the number of rows in the section.
return [self.myCategoryArray count];}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Category Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Category * categoryObj = [ops.categoryArray objectAtIndex:indexPath.row];
cell.textLabel.text = categoryObj.categoryName;
cell.textLabel.textAlignment = NSTextAlignmentCenter;//UITextAlignmentCenter;

return cell;}

@interface DbOperations : NSObject
{
sqlite3 * db;
}
//to add the contents of the database which will be the data source for the table
@property(nonatomic,strong)NSMutableArray * categoryArray;

//instance methods for ** Category Entity **

-(void) selectCategories;
-(void) insertCategory:(NSString *)categoryName;
-(void) updateCategory:(NSString *)categoryName:(NSString *)categoryName2;
-(void) deleteCategory:(NSString *)categoryName;
------------------------------------------
------------------------------------------
-(void) selectCategories{

categoryArray = [[NSMutableArray alloc] init];
//Get list of directories in Document path
NSArray * dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//Define new path for database
NSString * documentPath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"SmartSpeakerDB.sqlite"];
//By default there should only have one item in the array, so the index is always 0 unless you add subdirectories.

if(!(sqlite3_open([documentPath UTF8String], &db) == SQLITE_OK))
{
    NSLog(@"An error has occured.");

    return;
}else{//e2


    const char *sql = "SELECT categoryName FROM Category";
    //const char * sql, will be execute the SELECT statement.
    sqlite3_stmt *sqlStatement;


    if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"There is a problem with prepare statement Hah");
        return;
    }else{
        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
             NSLog (@"ENTER while");

 char * checkChar = (char*)sqlite3_column_text(sqlStatement,0);//1
    if (checkChar!= NULL) {
        NSString *s=[NSString stringWithUTF8String:checkChar];
                Category * newCategory = [[Category alloc] init];
        newCategory.categoryName = s;

             NSLog (@"***");
                [categoryArray addObject:newCategory];
            NSLog(@"Name %@",newCategory.categoryName);
               newCategory = nil;

     }//END IF
        }//END While
        sqlite3_finalize(sqlStatement);
        sqlite3_close(db);
    }//END ELSE 1
}//END ELSE 2
}//End
4

2 回答 2

0

我看起来可能是因为您正在使用:

[self.myCategoryArray count];

numberOfRowsInSection但是:

[ops.categoryArray objectAtIndex:indexPath.row];

cellForRowAtIndexPath.

如果两个数组有不同数量的对象,那么你就会遇到这个问题。您通常对这两个函数使用相同的数组,这样就不会发生这种情况。

于 2012-11-10T17:13:59.753 回答
0

您没有说明您在所有代码中实际遇到问题的位置,但我看到了一个可能的问题。在您的tableView:numberOfRowsInSection:方法中,您返回:

[self.myCategoryArray count];

但是然后在您的tableView:cellForRowAtIndexPath:方法中,您调用:

Category * categoryObj = [ops.categoryArray objectAtIndex:indexPath.row];

您在这里使用了两个不同的数组。这些应该基于相同的数据。

为了将来参考,在发布此类问题时,请指出异常来自的确切行,并仅发布相关代码。

于 2012-11-10T17:14:06.973 回答