2014-05-17 86 views
-3

我現在的問題是我在這段代碼上有數組。用JSON字典填充UITableView請求iOS

需要用URL請求替換UItableView JSON響應對象並替換字典值。

有一種方法可以清理信息並重新裝載表以刷新新數據或可以方便實現的數據?

#import "RecipeTableViewController.h" 
#import "RecipeTableCell.h" 
#import "RecipeDetailViewController.h" 
#import "Recipe.h" 

@interface RecipeTableViewController() 

@end 

@implementation RecipeTableViewController 
{ 
    NSArray *recipes; 
    NSArray *searchResults; 

    NSMutableArray *myObject; 
    // A Dictionary Object 
    NSDictionary *dictionary; 

    NSString *name; 
    NSString *subject; 
    NSString *avatar; 
    NSString *rate; 
    NSString *bio; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    name = @"name"; 
    subject = @"subject"; 
    avatar = @"avatar"; 
    rate = @"rate"; 
    bio = @"bio"; 

    myObject = [[NSMutableArray alloc] init]; 
    NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://latamig.com/data.json"]]; 

    id jsonObjects = [NSJSONSerialization JSONObjectWithData: jsonSource options:NSJSONReadingMutableContainers error:nil]; 


    for (NSDictionary *dataDict in jsonObjects) 
    { 
     NSString *name_data = [dataDict objectForKey:@"name"]; 
     NSString *subject_data = [dataDict objectForKey:@"subjects"]; 
     NSString *avatar_data = [dataDict objectForKey:@"avatar_s"]; 
     NSString *rate_data = [dataDict objectForKey:@"user_rate"]; 
     NSString *bio_data = [dataDict objectForKey:@"bio"]; 

     NSLog(@"Name: %@", name_data); 
     NSLog(@"Subject: %@",subject_data); 
     NSLog(@"Avatar: %@", avatar_data); 
     NSLog(@"Rate: %@",rate_data); 
     NSLog(@"Bio: %@",bio_data); 

     dictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
         name_data,name, 
         subject_data,subject, 
         avatar_data,avatar, 
         rate_data,rate, 
         bio_data, bio, nil]; 

     [myObject addObject:dictionary]; 
    } 



    // API CALL MAKE END 

    /* Initialize the recipes array 
    Recipe *recipe1 = [Recipe new]; 
    recipe1.name = @"Andruw"; 
    recipe1.prepTime = @"$255/h"; 
    recipe1.bio = @"I'm a Web developer working for 3 years as a Programmer on Facebook"; 
    recipe1.image = @"avatar1.jpg"; 


    recipes = [NSArray arrayWithObjects:recipe1, nil]; 

    */ 



} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [searchResults count]; 

    } else { 
     return [recipes count]; 
    } 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 71; 
} 

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

    // Configure the cell... 
    if (cell == nil) { 
     cell = [[RecipeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Display recipe in the table cell 
    Recipe *recipe = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     recipe = [searchResults objectAtIndex:indexPath.row]; 
    } else { 
     recipe = [recipes objectAtIndex:indexPath.row]; 
    } 

    cell.nameLabel.text = recipe.name; 
    cell.bioLabel.text = recipe.bio; 
    cell.thumbnailImageView.image = [UIImage imageNamed:recipe.image]; 
    cell.thumbnailImageView.clipsToBounds = YES; 
    cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width /2; 
    cell.prepTimeLabel.layer.cornerRadius = 5; 
    cell.prepTimeLabel.text = recipe.prepTime; 

    return cell; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) { 
     NSIndexPath *indexPath = nil; 
     Recipe *recipe = nil; 

     if (self.searchDisplayController.active) { 
      indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; 
      recipe = [searchResults objectAtIndex:indexPath.row]; 
     } else { 
      indexPath = [self.tableView indexPathForSelectedRow]; 
      recipe = [recipes objectAtIndex:indexPath.row]; 
     } 

     RecipeDetailViewController *destViewController = segue.destinationViewController; 
     destViewController.recipe = recipe; 
    } 
} 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText]; 
    searchResults = [recipes filteredArrayUsingPredicate:resultPredicate]; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 


@end 
+0

所以據我所知,Xcode沒有專門的設備來處理JSON。 –

+0

(你也許是指iOS或OSx?) –

+0

你有問題嗎? –

回答

1

目前還不清楚你問什麼,但這些代碼片段可以幫助:

@property (nonatomic, strong) NSData *rawFromCloud; 

#define yourUrl [NSURL URLWithString:@"http://blah.com/json/stubs"] 

-(void)fromCloud:(void(^)(void))after 
    { 
    dispatch_async(dispatch_get_main_queue(), 
     ^{ 
     self.rawStubsFromCloud = [NSData dataWithContentsOfURL:yourUrl]; 
     after(); 
     }); 
    } 

NSError* error; 
NSDictionary* jdic = [NSJSONSerialization 
    JSONObjectWithData:self.rawFromCloud 
    options:kNilOptions 
    error:&error]; 

//CRITICAL -- do this, to see what is going on: 
NSLog(@"%@", jdic); //to see the fields available 

NSArray* stubs = [jdic objectForKey:@"stubs"]; 

就是這樣 - 你有一個數組!

for (NSDictionary *stub in stubs) 
    { 
    NSString *nn = [stub objectForKey:@"userName"]; 
    NSLog("one userName %@", nn); 
    } 

(腳註,你也有「新」語法存根[@「username」的]但是「objectForKey」語法是初學者容易,我想。乾杯,希望它幫助。)

+0

如果不清楚,代碼片段可能如何幫助? –

+0

嘿@Leo!它有點**不清楚,但並不完全不清楚。我認爲這非常確定:Eddwin對如何使用「name」:[]類型的字段感到困惑,並且使用它填充NSArray。如果是這樣,上述應該爲他完成。 **我認爲英語不是Eddwin的第一語言,我們必須合理嗎?** – Fattie

+0

定義合理。我將它定義爲StackOverflow,以回答精心製作的問題。這種家庭作業問題,再加上格式不正確的問題(不管原因),都不應該受到鼓勵。 –