2013-05-12 52 views
0

iOS的新手。我正在使用AFNetworking下載JSON數據並將數據放入Dictionary對象中。這在下面的代碼完成:如何使用AFNetworking使用填充了JSON數據的NSDictionary對象填充原型單元格文本標籤

-(void)locationRequest 
{ 
NSURL *url = [NSURL URLWithString:@"http://sitespec/php/getlocations.php"]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
//AFNetworking asynchronous url request 
AFJSONRequestOperation *operation = [AFJSONRequestOperation 
            JSONRequestOperationWithRequest:request 
            success:^(NSURLRequest *request, NSHTTPURLResponse   *response, id responseObject) 
            { 

             self.locationsFromJSON = (NSDictionary *)responseObject; 

             NSLog(@"JSON RESULT %@", responseObject); 
             NSLog(@"JSON DICTIONARY %@", _locationsFromJSON); 
             [self.tableView reloadData]; 
            } 
            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseObject) 
            { 
             NSLog(@"Request Failed: %@, %@", error, error.userInfo); 
            }]; 

[operation start]; 

} 

我得到的數據和填充字典陣列作爲的NSLog的outpur看到以下稱:

2013-05-12 12:44:08.851 sitespec[2024:c07] JSON RESULT (
    { 
    city = "Rocky Mount"; 
    id = 1; 
    name = "Rocky Mount"; 
    phone = "(252) 451-1811"; 
    state = NC; 
    "street_address" = "620 Health Drive"; 
    zip = 27804; 
}, 
    { 
    city = "Elizabeth City"; 
    id = 2; 
    name = "Elizabeth City"; 
    phone = "(252) 335-4355"; 
    state = NC; 
    "street_address" = "109 Corporate Drive"; 
    zip = 27906; 
} 
) 
2013-05-12 12:44:08.852 sitespec[2024:c07] JSON DICTIONARY (
    { 
    city = "Rocky Mount"; 
    id = 1; 
    name = "Rocky Mount"; 
    phone = "(252) 451-1811"; 
    state = NC; 
    "street_address" = "620 Health Drive"; 
    zip = 27804; 
}, 
    { 
    city = "Elizabeth City"; 
    id = 2; 
    name = "Elizabeth City"; 
    phone = "(252) 335-4355"; 
    state = NC; 
    "street_address" = "109 Corporate Drive"; 
    zip = 27906; 
} 
) 

,但這裏是我失去了.....

如何我在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

滿足訪問Dictionary對象這個數據HOD?

類似:

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

cell.textLabel.text = [self.locationsFromJSON objectForKey:@"name"]; 

???

這不起作用,我得到一個SIGBART錯誤。我只是不知道如何讀取數據並填充表視圖?

我該怎麼做?任何幫助非常感謝.....

回答

1

我認爲這很通常我不明白人們爲什麼要從字典中提取數據並用字典內容填充表視圖的努力。我不明白爲什麼人們不爲JSON響應創建模型類,然後根據他們需要將模型添加到數組中。

所以湯米我強烈建議你創建一個名爲Address類模型(例如),並添加所有的JSON屬性作爲屬性模型類,當您分析您的JSON創建Address類的實例,你將增加到一個數組。在此之後,在你的表視圖類,你將有地址的陣列和:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section將返回yourAddressesArray.count

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { Address *address = [yourAddressesArray objectAtIndex:indexPath.row]; //assign your address properties to labels etc 

}

,當你有模型類很容易;)

EDIT FOR PARSING

從我看到的你的問題你有一個字典陣列。所以,我建議做這樣的事情:

在地址類中創建一個IMIT法josn:

-(id)initAddressWithJSON:(id)JSON { 
    slef = [super init]; 
    if(self) { 
     self.city = [JSON objectForKey:@"city"]; 
     // set all the properties here 
    } 
    return self; 
} 

當你收到名爲JSON響應,讓我們說「jsonResopnse」你應該這樣做:

-(NSArray*)myAddressesFromJSON:(id)JSON { 

    //you should add a validation here for JSON (not null not empty) 
    NSMutableArray *addresses = [[NSMutableArray alloc] init]; 
    Address *address; 
    for(NSDictionary *addressDict in JSON) { 
     address = [[Address alloc] initWithJSON:addressDict]; 
     [addresses addObject:address]; 
    } 
    return addresses; 
} 

注意,我從我的頭寫的代碼,我沒有用Xcode的:),並可能有一些構建錯誤(拼寫錯誤),但我希望你有這個想法。另外我想你正在使用JSONKit。

+0

我創建了一個地址模型,並添加了我的JSON數據的所有屬性。我已經將Address.h文件導入到我的tableView.m文件中,我在那裏下載數據並希望顯示tableView。我有一個名爲locationsFromJSON的數組,定義如上所述。儘管如此,你說「當你解析你的JSON時,你創建了Address類實例,你將添加到數組中」我在返回的值中有JSON響應:responseObject。我如何創建Address類實例並將它們添加到數組中? – 2013-05-12 18:15:25

+0

請檢查我編輯的答案 – danypata 2013-05-12 18:31:44

0

正如我所看到的,您在同一類中使用JSON請求和UITableView。在cellForRowAtIndexPath方法中設置斷點並檢查是否已有數據。如果不是,則:

詢問- (NSInteger)numberOfRowsInSection:(NSInteger)section方法或已經在numberOfSections如果您的字典已經填滿。否則返回0。

而且在收到JSON數據調用[tableView reloadData];

如果這不是問題的梅索德,請告訴你在哪裏得到的SIGBART錯誤;)

-1

感謝danypata提供了很好的反饋意見。我採取了一個稍微不同的路線來實現這一目標。讓我想到的是danypata的評論「看起來像你在你的JSON響應中有一系列詞典」......在AFNetworking塊中,我得到了JSON響應,我將JSON對象分配給定義爲屬性的數組當前TableVIewCOntroller。

-(void)locationRequest 

{ NSURL * URL = [NSURL URLWithString:@ 「myurl」];

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
//AFNetworking asynchronous url request 
AFJSONRequestOperation *operation = [AFJSONRequestOperation 
            JSONRequestOperationWithRequest:request 
            success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject) 
            { 

             self.locationsFromJSON = responseObject; 
             [self.tableView reloadData]; 
            } 
            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseObject) 
            { 
             NSLog(@"Request Failed: %@, %@", error, error.userInfo); 
            }]; 

[operation start]; 

} 

我也聲明瞭一個可變的字典財產和財產的NSString在我的JSON響應中的每個值。在下面的TableVIewCell方法中,我獲取位於index.row處的對象字典,並讀出鍵的值並將它們分配給原型單元格標籤屬性。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"locCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
if (cell == nil) { 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

} 


self.dictObj = [[NSMutableDictionary alloc] init]; 
self.dictObj = [_locationsFromJSON objectAtIndex:indexPath.row]; 
cell.textLabel.text = [_dictObj valueForKey:@"city"]; 
cell.detailTextLabel.text = [_dictObj valueForKey:@"phone"];  
return cell; 
} 

這工作!它只是讓別人指出明顯的「看起來像你有一個字典對象數組......」