我正在发布我尝试过的代码。我正在获取字符串值,然后我将它添加到一个数组并获取数组值。但是当我尝试在其他方法中使用数组值时,它的值是空白的。
//ViewController.h
@interface ViewController : UIViewController{
NSArray *totalData;
NSString *stringValueVideoiD;
NSMutableArray *TableVideoIDArray;
NSMutableArray *TableVideoNameArray;
}
@property (nonatomic,strong) IBOutlet UITableView *tableView;
@end
//ViewController.m
@implementation ViewController
@synthesize tableView;
- (void)viewDidLoad
{
TableVideoIDArray=[[NSMutableArray alloc]init];
TableVideoNameArray=[[NSMutableArray alloc]init];
[self getAllRows];
[tableView reloadData];
}
-(void) getAllRows{
if(sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK)
{
NSString *sqlStatement = [NSString stringWithFormat:@"SELECT * FROM table"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(db,[sqlStatement UTF8String] , -1, &compiledStatement, NULL)== SQLITE_OK)
{
while(sqlite3_step(compiledStatement)==SQLITE_ROW)
{
char *videoId = (char *) sqlite3_column_text(compiledStatement, 1);
stringValueVideoiD = [[NSString alloc] initWithUTF8String:videoId];
[TableVideoIDArray addObject:stringValueVideoiD];
NSLog(@"vids array:%@",TableVideoIDArray);
//vids array:value is printed
char *videoName = (char *) sqlite3_column_text(compiledStatement, 2);
NSString *stringValueVideoName = [[NSString alloc] initWithUTF8String:videoName];
[TableVideoNameArray addObject:stringValueVideoName];
totalData=[[NSArray alloc]initWithObjects:stringValueVideoiD,stringValueVideoName,nil];
}
}
sqlite3_finalize(compiledStatement);
sqlite3_close(db);
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [totalData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil) {
cell=[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(@"here:%@",TableVideoIDArray);
cell.channelTitle.text=[TableVideoIDArray objectAtIndex:indexPath.row];
//here comes the problem.My console result is
2013-09-18
11:18:24.431 AppName[468:11303] here:( )
2013-09-18 19:43:35.562 AppName[3991:11303] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'//so guys I need your help why this is happenning or I am making mistakes in my code.