我用: self performSelector:@ selector(loadData)withObject:nil ... 它看起来只在“loadData”中使用某些命令,但其余的不是。
这是我的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 :(得分: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
}
祝你好运!