2016-02-18 28 views
2

我正在學習iOS,我在向tableView中添加JSON URL時遇到了問題。我怎樣才能調用TableView?現在,只有在控制檯中顯示我的JSON網址。請幫幫我。 我不知道我在哪裏可以犯錯。如何將JSON URL responsedata加載到tableView?

// WordsViewController.m 


#import "WordsViewController.h" 
#import "Word.h" 


@interface WordsViewController() 

@end 

@implementation WordsViewController 
@synthesize jsonArray, wordsArray; 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    //set the title of our VC 
    self.title = @"SŁOWNIK"; 

    //LOAD DATA 
    [self simpleJsonParsing]; 

} 

#pragma mark - Table view data source 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 5; 
} 


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

    return cell; 
} 

- (void)simpleJsonParsing { 
    //-- Make URL request with server 
    NSHTTPURLResponse *response = nil; 
    NSString *jsonUrlString = [NSString stringWithFormat:@"https://uidictionary.herokuapp.com/phrases.json"]; 
    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    //-- Get request and response though URL 
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url]; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 

    //-- JSON Parsing 
    NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]; 

    NSData * data = [NSData dataWithContentsOfURL:url]; 

    NSLog(@"Result = %@",result); 
} 

@end 

控制檯告訴我:

2016-02-18 13:01:42.645 Słownik[20421:5341020] Result = { 
    phrases =  (
     { 
      expression = pejoratywny; 
      meaning = "\U201eCo\U015b, co jest pejoratywne, wyra\U017ca czyj\U0105\U015b negatywn\U0105 ocen\U0119 lub, rzadziej, samo jest przedmiotem takiej oceny\U201d.\U00a0Inny s\U0142ownik j\U0119zyka polskiego\U00a0PWN, z kt\U00f3rego przytoczy\U0142em t\U0119 definicj\U0119, podaje gar\U015b\U0107 cytat\U00f3w, w nich za\U015b znajdziemy takie po\U0142\U0105czenia wyrazowe, jak\U00a0znaczenie pejoratywne,okre\U015blenie pejoratywne,\U00a0cechy pejoratywne. Jak st\U0105d wynika, przymiotnik ten ma do\U015b\U0107 szeroki zakres u\U017cycia. \U0141\U0105cz\U0105c go ze s\U0142owem\U00a0zabarwienie, nie dublujemy jego tre\U015bci."; 
     }, 
     { 
      expression = "Ruby on Rails"; 
      meaning = "\U00a0(cz\U0119sto nazywany\U00a0RoR\U00a0lub po prostu\U00a0Rails) \U2013\U00a0framework\U00a0open source\U00a0do szybkiego tworzenia\U00a0aplikacji webowych\U00a0stworzony g\U0142\U00f3wnie przez du\U0144skiego programist\U0119\U00a0Davida Heinemeiera Hanssona\U00a0w ramach pracy nad oprogramowaniem\U00a0Basecamp. RoR zosta\U0142 napisany w j\U0119zyku\U00a0Ruby\U00a0z u\U017cyciem architektury\U00a0MVC\U00a0(ang.\U00a0Model-View-Controller)."; 
     }, 
    ); 
} 
+1

你是在相關的方式 –

+0

@ Anbu.Karthik是的,但我不知道接下來會發生什麼。你能解決我的問題嗎?感謝您的幫助 – mechu911

回答

4

不喜歡

創建一個NSMutableArray的iyour viewDidLoad中

@interface WordsViewController() 
{ 
NSMutableArray *finalResultArray; 
} 
@end 


- (void)viewDidLoad { 
[super viewDidLoad]; 
//set the title of our VC 
self.title = @"SŁOWNIK"; 

finalResultArray = [NSMutableArray new]; 
//LOAD DATA 
[self simpleJsonParsing]; 

}

//-- JSON Parsing 
NSArray *result = [[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]objectForKey:@"phrases"]; 

    [finalResultArray removeAllobjects]; 
NSLog(@"Result = %@",result); 
for (NSMutableDictionary *dic in result) 
{ 


NSMutableDictionary *temp = [NSMutableDictionary new]; 
[temp setObject:[tmp objectForKey:@"expression"] forKey:@"expression"]; 
[temp setObject:[tmp objectForKey:@"meaning"] forKey:@"meaning"]; 

[finalResultArray addObject:temp]; 
} 

if (finalResultArray) 
{ 
[self.tableView reloadData]; 
} 


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

    return finalResultArray.count; 
} 


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

    cell.textlabel.text = [[finalResultArray objectAtIndex:indexPath.row] objectForKey:@"expression"]; 

    return cell; 
} 
+0

我沒有時間,我現在離開辦公室,它的工作, –

+0

看到這一次,它也可以幫助你http://stackoverflow.com/questions/35478162/how-to-parse-diffrent-array-字典在每一個uicollectionviewcell/35478554#35478554 –

+0

你需要任何幫助問我,我等待你的迴應 –