0

我正在发布我尝试过的代码。我正在获取字符串值,然后我将它添加到数组中并获取数组值。但是当我尝试在其他方法中使用数组值时,它的值是空白的。

//ViewController.h
@interface ViewController : UIViewController{
     NSString *stringValueVideoiD;
     NSMutableArray *TableVideoIDArray;
}

//ViewController.m
@implementation ViewController

- (void)viewDidLoad
{
   TableVideoIDArray=[[NSMutableArray alloc]init];
    [self getAllRows];
}

-(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=[[NSMutableArray alloc]init];
                [TableVideoIDArray addObject:stringValueVideoiD];
                NSLog(@"vids array:%@",TableVideoIDArray);
           //vids aray:value is printed
}
}
}
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array 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);
//here comes the problem.My console result is
2013-09-18

11:18:24.431 TubeAlert[468:11303] here:( ) //所以伙计们,我需要你的帮助,为什么会发生这种情况,或者我的代码出错了。

4

1 回答 1

0

TableVideoIDArray 分配了两次。

在 viewDidLoad 中,使用

TableVideoIDArray=[NSMutableArray array]; 

不要在 While 循环中分配 with。

于 2013-09-18T06:13:25.083 回答