将数据从sqlitedatabase传递到Detailview

时间:2014-07-21 11:28:08

标签: ios


我正在创建一个iOS应用程序,它是一个目录系统,在第一页上我加载数据,例如tableview中的某些状态列表(来自sqlite数据库)。单击状态时,只有特定状态下的区域必须显示在详细视图的tableview中。我的问题是,当点击每个区时,该区的电话号码必须显示在下一个视图控制器中。有人可以帮忙吗?

这是第一次观看。

@implementation firstview
@synthesize ddetails=_ddetails;
@synthesize at,arraydata,show,table;
static firstview *_database;
+ (firstview*)database {
if (_database == nil) {
    _database = [[firstview alloc] init];
}
return _database;}


 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

        return [theauthors count];
}
   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 30;}

- (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];
}

menu *me = [self.theauthors objectAtIndex:indexPath.row];
cell.textLabel.text=me.state;
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = selectedCell.textLabel.text;
    [_button setTitle:cellText forState:UIControlStateNormal];
    menu *infos=[theauthors objectAtIndex:indexPath.row];
        self.ddetails =  [self.storyboard instantiateViewControllerWithIdentifier:@"detailViewController"] ;
    _ddetails.uniqueId=infos.id;
   // NSLog(@"%@",infos.state);
   // NSLog(@"%d",infos.id);
          [self.navigationController pushViewController:_ddetails animated:NO];
        }

-(NSMutableArray *) authorList{
theauthors = [[NSMutableArray alloc]init] ;
show=[[NSMutableArray alloc]init];
@try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath   ]stringByAppendingPathComponent:@"menu.sqlite"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

    }
    NSString *query =[NSString stringWithFormat:@"SELECT id,category FROM  category "];

    sqlite3_stmt *selectstmt;

    if(sqlite3_prepare_v2(db, [query UTF8String], -1, &selectstmt, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }else{

        while (sqlite3_step(selectstmt)==SQLITE_ROW) {
            menu * men = [[menu alloc] init];
            men.id= sqlite3_column_int(selectstmt,0);
            men.state=[NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt,1)];
            [theauthors addObject:men];
           // NSLog(@"%@",men.category);

           // NSLog(@"%d",men.id);
        }
    }
}

@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
         sqlite3_close(db);
    return theauthors;
}

}
- (detailsnews *)Detailsnews:(int)uniqueId{
NSLog(@"%d",uniqueId);
detailsnews *get=nil;
_the=[[NSMutableArray alloc]init];
_gets=[[NSMutableArray alloc]init];
@try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"menu.sqlite"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

    }
    NSString *sqls =[NSString stringWithFormat:@"SELECT * FROM  menulist where category=%d",uniqueId];

   // NSLog(@"%d",uniqueId);
    sqlite3_stmt *selectstmt6;


    if(sqlite3_prepare(db, [sqls UTF8String], -1, &selectstmt6, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }else{


        while (sqlite3_step(selectstmt6)==SQLITE_ROW) {
           // NSLog(@"%d",uniqueid);
          int totalValue = 0;
            int uniqueId = sqlite3_column_int(selectstmt6,0);
          //  NSLog(@"%d",uniqueid);
           [_gets addObject:[NSNumber numberWithInt:uniqueId]];
            for(NSNumber *number in _gets) // Use fast enumeration to iterate through the array
                {
                  totalValue = [number intValue];
                // NSLog(@"%d",totalValue);

                 }
            char *nameChars = (char *) sqlite3_column_text(selectstmt6, 2);
            NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
            char *rec = (char *) sqlite3_column_text(selectstmt6, 1);
            NSString *rece = [[NSString alloc] initWithUTF8String:rec];
          //  NSLog(@"%@",rece);
           [_the addObject:rece];
           NSMutableString * result = [[NSMutableString alloc] init];
            for (NSObject * obj in _the)
                 {
                 [result appendString:[obj description]];
                  result = [_the componentsJoinedByString: @"\r"];
                // NSLog(@"%@",result);
                 }
            //[_the addObject:[NSNumber numberWithInt:uniqueid]];
           // int totalValue = 0;
           // for(NSNumber *number in _the) // Use fast enumeration to iterate through the array
           // {
              //  totalValue = [number intValue];
               // NSLog(@"%d",totalValue);

           // }
           //
         //   NSMutableString * result = [[NSMutableString alloc] init];
           // int totalValue = 0;

            //for (NSObject * obj in _the)
           // {
           // [result appendString:[obj description]];
            //  result = [_the componentsJoinedByString: @"\r"];

               // NSLog(@"%@",result);

           // }
                            //NSArray *arr=[result componentsSeparatedByString:@"\r"];
           // NSMutableString * vgh = [[NSMutableString alloc] init];
           // for (NSObject * obj in arr)
           // {
                //[vgh appendString:[obj description]];
               // vgh = [arr componentsJoinedByString: @"\r"];

                         // }

            get = [[detailsnews alloc] initWithUniqueId:totalValue district:result phonenumber:name ];
            //NSLog(@"%@",_the);

        }

        }
}
 @catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
     sqlite3_close(db);
    return get;
}

}

detailview controller

    - (void)viewDidLoad
{
    [super viewDidLoad];


        detailsnews *details = [[firstview database]
Detailsnews:_uniqueId];

    if (details != nil) {
        NSArray *arrComponents = [details.district componentsSeparatedByString:@"\r"];
        arr = [[NSMutableArray alloc] init];
        for(int i = 0; i < [arrComponents count] ;i++)
        {
        NSString *str = [arrComponents objectAtIndex:i];

        [arr addObject:str];



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [arr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath     {
return 30;}
 - (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];
}
detailsnews *details = [[firstview database]
Detailsnews:_uniqueId];
cell.textLabel.text=[arr objectAtIndex:indexPath.row];
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
[button2 setTitle:cellText forState:UIControlStateNormal];
detailsnews *details = [[firstview database]

                        Detailsnews:_uniqueId];
//NSLog(@"%d",details.uniqueId);
   // NSLog(@"%@",details.rece);
    // detailsnews *infos=[arr objectAtIndex:indexPath.row];
    self.ddetails =  [self.storyboard     instantiateViewControllerWithIdentifier:@"phonelistViewController"] ;
  _ddetails.uniqueId=details.uniqueId;
   // NSLog(@"%d",details.uniqueId);
    [self.navigationController pushViewController:_ddetails animated:NO];
    }

1 个答案:

答案 0 :(得分:0)

你需要将包括phoneNumbers的数据从第一个stateViewController传递到District然后从District传递给DetailsPage ...我建议传递一个NSdictionary,其中包含从一个视图到另一个视图的数据,例如......在第二个ViewController中声明一个全局字典as mainDictionary ..

并在didSelectRowAtIndexPath中将数据传递为

 self.ddetails =  [self.storyboard     instantiateViewControllerWithIdentifier:@"phonelistViewController"] ;
  _ddetails.mainDict=AllThe Details;

使用PhoneNumbers做同样的事情