我在iOS上遇到以下代码的问题,我有3个部分,每个部分都有一定数量的行,由函数定义:countcells。
但每次我运行它都有同样的错误: 2014-07-29 22:16:32.710 TYM-APP [5598:60b] *由于未捕获的异常终止应用程序' NSRangeException',原因:' * - [ __NSArrayI objectAtIndex:]:索引2超出边界[0 .. 0]'
代码有什么问题??
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT count(*) FROM homedata where module_ID= 77"];
return [self countcells:querySQL];
}
else if(section == 1)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT count(*) FROM homedata where module_ID= 78"];
return [self countcells:querySQL];
}
else if (section == 2)
{
return[self countcells:@"SELECT count(*) FROM homedata where module_ID= 81"];
}
else return 0;
}
- (int) countcells: (NSString *)query
{
int e;
NSString* text;
NSString * paths=[self getWritableDBPath];
const char *dbpath = [paths UTF8String];
sqlite3_stmt *statement;
static sqlite3 *database = nil;
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:query];
const char *query_stmt = [querySQL UTF8String];
// NSLog(@"Databasae opened = %@", userN);
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
text = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
e = sqlite3_column_int(statement, 0);
NSLog( @"Valeur directe: %d", e);
}
// username.text=@"No Username";
sqlite3_finalize(statement);
}
// NSLog( @"Save Error: %s", sqlite3_errmsg(database) );
NSLog( @"Count Value: %@", text);
sqlite3_close(database);
}
return e;
}
答案 0 :(得分:1)
int e有垃圾值。确保它应该是> = 0值。