0

我使用: self performSelector:@selector(loadData) withObject:nil ... 看起来只在“loadData”中使用某些命令,但其余的不是。

这是我的viewdidload:

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

这是加载数据:

 -(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”中,它将数据加载到表中。

请提供任何建议或帮助。

4

1 回答 1

1

一些事情:如果你看到任何 NSLog 输出,那么 performSelector 是成功的。您应该更改问题的标题。

如果您尝试将数据加载到表中,则该方法应以告诉 UITableView 重新加载数据(或使用开始/结束更新更精细的加载)结束。

如果 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
}

祝你好运!

于 2012-06-19T03:19:19.210 回答