簡體   English   中英

JSON數據UITableView到UITableView

[英]JSON data UITableView to UITableView

我不太擅長編碼,我的代碼中有一個嚴重的問題,我想顯示我的json數據從一個表到另一個表,例如:在第一個表格視圖中,我想顯示“學生”的名字,而名字后面是單擊,它將跳到下一個表格視圖,並顯示該學生已被選中的課程(一個學生可能有一個以上的課程),那么我該怎么做呢?這是第一個表格視圖中的mycode

- (void)viewDidLoad
{
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this     view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.Title = @"選擇類別";
//    classify = [[NSArray alloc] initWithObjects: @"時尚類",@"飲食類",@"娛樂類",@"文創類",   nil];
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"DataList" ofType:@"plist"];
//    classify_ = [[NSMutableArray alloc] initWithContentsOfFile : path];
NSURL *url = [NSURL URLWithString:@"http://localhost/store.php"];
NSData *jsonData=[NSData dataWithContentsOfURL:url];
NSError *error = nil;

classify_ = [NSJSONSerialization JSONObjectWithData:jsonData options:
        NSJSONReadingMutableContainers error:&error];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

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

//table cell count
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.classify count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

 cell.textLabel.text = [[self.classify objectAtIndex:indexPath.row]   objectForKey:@"classify"];

return cell;


}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"Second"])
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    //NSManagedObject *object = [[self fetchedResultsController]   objectAtIndexPath:indexPath];
    [[segue destinationViewController] setSecondClassify:[self.classify objectAtIndex:indexPath.row]];
}
}

tableView1.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {

        NSIndexPath *indexPath = [myTable indexPathForSelectedRow];
        NSDictionary *item = [displayItems objectAtIndex:[indexPath row]];

        [[segue destinationViewController] setDetailItem:item];
    } 
}

tableView2.h:

@property (strong, nonatomic) id detailItem;

tableView2.m:

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }
}

- (void)configureView
{
    if (self.detailItem) {
       itemID = self.detailItem;
       [self getCourse];
    }
}

- (void)getCourse{

      NSString *code = [NSString stringWithFormat:@"user=%@", itemID];
      NSData *postData = [code dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
      NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
      NSURL *url = [NSURL URLWithString:@"http://localhost/getUser.php"];

      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
      [request setHTTPMethod:@"POST"];
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
      [request setHTTPBody:postData];

      NSURLResponse *response;
      NSError *error;
      NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

      allCourseArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

      NSDictionary *item = [allCourseArray objectAtIndex:0];

      NSString *courseName = [item objectForKey:@"courseName"];
      NSString *courseCode = [item objectForKey:@"courseCode"];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM