1

データベース全体を変更しました...

これで、下に表示される 2 つのテーブルができました

最初のテーブル:- カテゴリ テーブル

Cat_id         CategoryName 
====================================
  1                  Jesus
  2                 Buddha
  3                BillGates

2 番目のテーブル :- SayingTable

Say_id    Cat_id        Sayings 
===================================
1          1         Never Say False
2          1         God Bless you 
3          1         Be a Good Boy
4          2         Make a Peaceful World
5          2         Never Lie with the People

そして、以下の画像のように表示される Firstview Controller で UITableView を使用しました

イエス・キリストのセルをクリックすると..またはその他のセルがクラッシュします..

私のコードは以下です

FirstViewController.m

- (void)viewDidLoad
{

 dataaray = [[NSMutableArray alloc] init];;

    [dataaray addObject:@"Jesus Christ"];
    [dataaray addObject:@"Budhdha"];
    [dataaray addObject:@"BillGates"];

     jesus = [[NSMutableArray alloc] init];


    Buddha = [DatabaseOperation SelectCategory:2];
    Buddha = [Buddha valueForKey:@"Saying"];
    NSLog(@"Buddha Bhagwan Category=%@",Buddha);

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"Cellidentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

   if (cell==nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.textLabel.text = [dataaray objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
   return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *mydetail = [[DetailViewController alloc]   initWithNibName:@"DetailViewController" bundle:nil];
    if ([[dataaray objectAtIndex:indexPath.row] isEqual:@"Jesus Christ"])
    {
        mydetail.flag=1;
       int SelectedRow = (indexpath.row+1);            

       jesus = [DatabaseOperation SelectCategory:SelectedRow];
       jesus = [jesus valueForKey:@"Saying"];
        NSLog(@"Jesus Category===%@",jesus);

      mydetail.Jesus2Array = [jesus mutablecopy];   //Note Jesus2 is an Array of detailviewcontroller
    }
   else if ([[dataaray objectAtIndex:indexPath.row] isEqualToString:@"Budhdha"])
    {
        mydetail.flag=2;
       SelectedRow = (indexpath.row+1);
      Buddha = [DatabaseOperation SelectCategory:SelectedRow];
       jesus = [jesus valueForKey:@"Saying"];
        NSLog(@"Jesus Category===%@",jesus);
        mydetail.Buddha2Array = [Buddha mutableCopy]; //Note Jesus2 is an Array of  detailviewcontroller
    }

DetailviewController.m

- (void)viewDidLoad
{

    NSLog(@"Flag Value===%d",flag);

    NSLog(@"Jesus 2 Array===%@",Jesus2Array);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (flag==1)
    {
        return [Jesus2Array count];
    }
   if(flag==2)
    {
          return [Buddha2array count];
    }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
 if (flag==1)
    {
       cell.textLabel.text = [Jesus2Array objectAtIndex:indexPath.row];
    }

データベース操作.h

 #import <Foundation/Foundation.h>
    #import "DatabaseOperation.h"
    #import <sqlite3.h>

    @interface DatabaseOperation : NSObject
    {

    }

    +(void)Check_Create_Database;
    +(NSMutableArray*)SelectCategory:(int)Category_id;

データベース操作.m

 #import "DatabaseOperation.h"
    #import <sqlite3.h>


    static NSString *dbPath;
    static NSString *databaseName=@"Message.sqlite";


    @implementation DatabaseOperation


    +(void)Check_Create_Database
    {
        dbPath =[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:databaseName];

        BOOL success;

        NSFileManager *fm=[NSFileManager defaultManager];
        success=[fm fileExistsAtPath:dbPath];
        if(success){
            return;
        }
        NSString *dbPathFromApp=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

        NSLog(@"dbPathFromApp = %@",dbPathFromApp);

        [fm copyItemAtPath:dbPathFromApp toPath:dbPath error:nil];
    }
    +(NSMutableArray*)SelectCategory:(int)Category_id
    //+(NSMutableArray*)SelectedCategory
    {
        sqlite3 *database;

        NSMutableArray *dic=[[NSMutableArray alloc] init];

        NSString *SAYING;

        if(sqlite3_open([dbPath UTF8String] , &database) == SQLITE_OK)
        {
            NSString *sqlTmp=[NSString stringWithFormat:@"Select * from SayingTable Where Cat_id=%d",Category_id];
            const char *sqlStmt=[sqlTmp UTF8String];
            sqlite3_stmt *cmp_sqlStmt;

            if(sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt, NULL)==SQLITE_OK)
            {
                while(sqlite3_step(cmp_sqlStmt)==SQLITE_ROW)
                {
                    NSLog(@"SQL Statements====%s",sqlStmt);

                        SAYING=[NSString stringWithUTF8String:(char *) sqlite3_column_text(cmp_sqlStmt, 2)];

                        NSMutableDictionary *dicObj=[[NSMutableDictionary alloc] init];

                        [dicObj setValue:[NSString stringWithFormat:@"%@",SAYING] forKey:@"Saying"];

                        [dic addObject: dicObj];



                }
            }

            sqlite3_finalize(cmp_sqlStmt);


        }
        sqlite3_close(database);

セルをクリックすると、Detailview Controller が読み込まれません.....コードのどこが間違っているのかわかりません。助けてください..

ありがとうございました...

4

0 に答える 0