【问题标题】:self performSelector:@selector(loadData) withObject:nil - not workingself performSelector:@selector(loadData) withObject:nil - 不工作
【发布时间】:2012-06-19 02:36:06
【问题描述】:

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

请提供任何建议或帮助。

【问题讨论】:

  • 你试过在loadData方法的第一行调试和设置断点吗?
  • 它只能打印这两行:NSLog(@"Start LoadData") 和 NSLog(@"Finish loaData") 但如果我复制“loadData”中的所有代码并传递给“viewDidLoad”它向我展示了带有数据的表格

标签: iphone self performselector


【解决方案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
}

祝你好运!

【讨论】:

  • 我尝试工作的代码只是将数据加载到“listOfItems”,所以请提供更多帮助(参见上面的“loadData”)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
相关资源
最近更新 更多