2012-06-19 67 views
0

我使用: self performSelector:@selector(loadData)withObject:nil ... 它看起來像只在「loadData」中使用某些命令,但是休息不是。self performSelector:@selector(loadData)withObject:nil - not working

這裏是我的viewDidLoad:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [mActivity startAnimating]; 
    [self performSelector:@selector(loadData) withObject:nil afterDelay:2]; 
    //[mActivity stopAnimating]; 
} 

這裏是loadData:

-(void)loadData 
{ 
    [mActivity startAnimating]; 
    NSLog(@"Start LoadData"); 
    AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    NSString *selectData=[NSString stringWithFormat:@"select * from k_proverb ORDER BY RANDOM()"]; 
    qlite3_stmt *statement; 
    if(sqlite3_prepare_v2(delegate.db,[selectData UTF8String], -1,&statement,nil)==SQLITE_OK){ 
     NSMutableArray *Alldes_str = [[NSMutableArray alloc] init]; 
     NSMutableArray *Alldes_strAnswer = [[NSMutableArray alloc] init]; 
     while(sqlite3_step(statement)==SQLITE_ROW) 
     { 
      NSString *des_strChk= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,3)]; 
      if ([des_strChk isEqualToString:@"1"]){ 
       NSString *des_str= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,4)]; 
       [Alldes_str addObject:des_str]; 
      } 
     } 
     Alldes_array = Alldes_str; 
     Alldes_arrayAnswer = Alldes_strAnswer; 
    }else{ 
     NSLog(@"ERROR '%s'",sqlite3_errmsg(delegate.db)); 
    } 
    listOfItems = [[NSMutableArray alloc] init]; 
    NSDictionary *desc = [NSDictionary dictionaryWithObject: 
          Alldes_array forKey:@"description"]; 
    [listOfItems addObject:desc]; 
    //[mActivity stopAnimating]; 
    NSLog(@"Finish loaData");} 

它給我只能打印2線,但沒有加載我的數據表中,但如果我複製所有的代碼從「loadData」內部和過去的「viewDidLoad」,它將數據加載到表中。

請任何建議或幫助。

+0

您是否嘗試過在loadData方法的第一行調試並設置斷點? – danielbeard

+0

它只能打印2行: NSLog(@「Start LoadData」)和NSLog(@「Finish loaData」) 但如果我複製「loadData」中的所有代碼並傳入「viewDidLoad」 它會顯示給我包含數據的表格 – Sunny

回答

1

幾件事情:如果你看到任何NSLog輸出,那麼performSelector成功。你應該改變你的問題的標題。

如果您試圖將數據加載到表中,則該方法應以告知UITableView reloadData(或使用開始/結束更新的更精細的加載)結束。

如果LISTOFITEMS是支持表中的數據,得到這個像這樣通過硬編碼的東西第一個工作:

-(void)loadData { 

    listOfItems = [NSArray arrayWithObjects:@"test1", @"test2", nil]; 
    [self.tableView reloadData]; 
    return; 

    // keep all of the code you wrote here. it won't run until you remove the return 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSString *string = [listOfItems objectAtIndex:indexPath.row]; 
    cell.textLabel.text = string; 
    return cell; 

    // keep all of the code you probably wrote for this method here. 
    // as above, get this simple thing running first, then move forward 
} 

祝你好運!

+0

我嘗試使用它的代碼只是將數據加載到「listOfItems」,因此還需要其他幫助(請參閱上面的「loadData」)。 – Sunny

+0

非常感謝。現在正在工作。 – Sunny