iOS tableview多个部分
我对iOS上的以下代码有一个问题,我有3个部分,每个部分都有一定数量的行,这是由函数countcells定义的 但每次我运行它时都会出现相同的错误: 2014-07-29 22:16:32.710 TYM-APP[5598:60b]*由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*-[\uu NSArrayI objectAtIndex:]:索引2超出边界[0..0]” 代码有什么问题iOS tableview多个部分,ios,tableview,rows,sections,Ios,Tableview,Rows,Sections,我对iOS上的以下代码有一个问题,我有3个部分,每个部分都有一定数量的行,这是由函数countcells定义的 但每次我运行它时都会出现相同的错误: 2014-07-29 22:16:32.710 TYM-APP[5598:60b]*由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*-[\uu NSArrayI objectAtIndex:]:索引2超出边界[0..0]” 代码有什么问题 - (NSInteger)numberOfSectionsInTableVi
- (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;
}
int e具有垃圾值。确保它的值应大于等于0。Yikes!这些表方法被反复调用。执行一次查询并保存结果。多次打开和关闭数据库是非常低效的,因为不必要地反复进行这些查询。哪行代码导致了错误?它没有进入第一和第二个条件。它只进入第三个条件(如果节==2)并在那里崩溃。对于section=0和section=1,它没有访问它们。我修复了一些问题,但现在错误是索引1超出了界限[0..0]。在故事板中,我创建了一个包含3个部分的表。是否应该工作?请再说一遍-哪行代码导致了错误?