1

didSelectRowAtIndexPath:即使在将表委托和数据源连接到文件所有者之后,方法也没有调用。

当我尝试打印在此方法中未打印的内容时,请在下面找到我的代码供您参考:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [favorite count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell= nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    if (cell == nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];}

    db * temp =(db *)[self.favorite objectAtIndex:indexPath.row];
    cell.textLabel.text=temp.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       NSLog(@"b");

        UITableViewCell *selectedCell = [secondTable cellForRowAtIndexPath:indexPath];
    [secondTable.delegate tableView:secondTable didSelectRowAtIndexPath:indexPath];
        cellText = selectedCell.textLabel.text;
        NSLog(@"%@",cellText);
        [fav addObject:cellText];       
    arrayTwo = tableTw.tableTwo;
    msg = [NSString stringWithFormat: @" %@  ",[arrayTwo objectAtIndex:0]];
        NSLog(@"%@",[arrayTwo objectAtIndex:0]);

}

-(void)checkAndCreateDatabase{
    BOOL success;
    NSFileManager *fileManager=[NSFileManager defaultManager];
    success=[fileManager fileExistsAtPath:databasePath];
    if(success)
        return;

    NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}   
-(IBAction)resPage:(id)sender{
    NSLog(@"%@",fav);
   sqlite3 *database;
    favorite=[[NSMutableArray alloc]init];
    if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){
         NSString *sql=[NSString stringWithFormat:@"SELECT name,item_country.id,text.text FROM country,item_country,text WHERE country.name ='%@' AND text.item = item_country.item AND country.id = item_country.id", cellText];
        const char *sqlStatement = [sql UTF8String];;
        sqlite3_stmt *compiledStatement;
        NSLog(@"a");
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK){
            NSLog(@"A");
            while (sqlite3_step(compiledStatement)==SQLITE_ROW) {
                    d=[NSString stringWithCString:(char *)sqlite3_column_text(compiledStatement, 2) encoding:NSUTF8StringEncoding];
                  a= [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                db *info =[[db alloc]initWithText:(NSString *)d andAg:(NSString *)a];
                [favorite addObject:info];
            }            
        }
        sqlite3_finalize(compiledStatement);

    sqlite3_close(database);


        if(favorite.count >0){
        txt.text = [NSString stringWithFormat:@"%@ ", [favorite objectAtIndex:0] ];

        }}
}
- (void)viewDidLoad {
    {
        databaseName=@"nobel10.db";
        NSArray *documentPaths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentDir = [documentPaths objectAtIndex:0];
        databasePath=[documentDir stringByAppendingPathComponent:databaseName];
        [self checkAndCreateDatabase];
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    [super viewDidLoad];
    if (tableOn == nil) {
        tableOn = [[table1 alloc] init];
    }
    if (tableTw == nil) {
        tableTw = [[table2 alloc] init];
    }
    if (tableThre == nil) {
        tableThre = [[table3 alloc] init];
    }
    if (tableFou == nil) {
        tableFou = [[table4 alloc] init];
    }

    [firstTable setDataSource:tableOn];
    [secondTable setDataSource:tableTw];
    [thirdTable setDataSource:tableThre];
    [fourthTable setDataSource:tableFou];
    [fifthTable setDataSource:tableFiv];

    [firstTable setDelegate:tableOn];
    [secondTable setDelegate:tableTw];
    [thirdTable setDelegate:tableThre];
    [fourthTable setDelegate:tableFou];


    tableOn.view = tableOn.tableView;
    tableTw.view = tableTw.tableView;
    tableThre.view = tableThre.tableView;
    tableFou.view = tableFou.tableView;


    {fav=[[NSMutableArray alloc]init];
         cellText =[[NSString alloc]init];
        secondTable=[[UITableView alloc]init];
        secondTable.delegate=self;
        secondTable.dataSource=self;
        secondTable.tag = 20;
    [self.view addSubview:secondTable];
    }
}

请提出建议!

4

2 回答 2

1

我认为 secondTable 不是你工作的对象。确保它。

于 2012-04-17T06:19:37.777 回答
0

添加 .h 文件并在 dalegate 调用时调用 dataSource。

obj_TableView.delegate = self;
obj_TableView.dataSource = self;

那就试试吧。

于 2012-04-17T06:28:50.567 回答