2013-10-19 46 views
0

填充一個的UITableView我有一個JSON其迴應一個的NSDictionary:從NSDictionary的

({ 
    email = "[email protected]"; 
    name = "User1"; 
}, 
    { 
    email = "[email protected]"; 
    name = "user2"; 
}) 

這是我在德.m文件代碼:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 



    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 

    int errorCode = httpResponse.statusCode; 

    NSString *fileMIMEType = [[httpResponse MIMEType] lowercaseString]; 

    NSLog(@"response is %d, %@", errorCode, fileMIMEType); 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 

    //NSLog(@"data is %@", data); 

    NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

    //NSLog(@"string is %@", myString); 
    NSError *e = nil; 

    usersDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e]; 

    NSLog(@"dictionary is %@", usersDictionary); 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 



    // inform the user 

    NSLog(@"Connection failed! Error - %@ %@", 

      [error localizedDescription], 

      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    NombreUsuario = [[NSMutableArray alloc] initWithCapacity:[usersDictionary count]]; 


} 

我用這個來回報在部分行數:

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

但我不知道如何把對數據的電子郵件,名字變成了實現代碼如下,其中每個CE必須顯示名稱的主標籤和電子郵件的副標題。

在此先感謝。

+0

你顯示的JSON是一個字典數組......你有什麼具體問題? – Wain

+1

這確實是一個基本問題。鑑於你已經意識到你有一個'NSArray'的NSDictionary'對象(正如Wain指出的那樣),它非常適合表格視圖數據源,你可以在這裏找到答案:[Table View Programming Guide for iOS](https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html),尤其是章節「仔細觀察表格視圖單元格」。 – CouchDeveloper

+0

對不起,提出這樣一個基本問題,但我找不到如何從字典中獲取nsarray,我總是將一個「[__NSArrayM objectForKey:]:無法識別的選擇器發送到實例」我真的不知道如何處理與此同時,我一直在閱讀所有的網絡兩天。 – rafarq

回答

1

我會回答我自己的問題,以供將來參考誰可能需要它。

首先,我必須將json響應存儲在NSMutableArray而不是字典中。

由於我有一個字典數組,我有兩個級別的信息,我試圖將它分解成新的對象,但它不是正確的方法,所以要訪問第二級,我必須導航到它:

cell.textLabel.text = [[NombreUsuario objectAtIndex:indexPath.row]objectForKey:@"name"]; 

有了這個[NombreUsuario objectAtIndex:indexPath.row]我訪問的信息的第一級,然後用objectForKey:@「名」我得到的所有匹配的鍵「name」的數值。

對於細節標籤文本是一樣的:

cell.detailTextLabel.text = [[NombreUsuario objectAtIndex:indexPath.row]objectForKey:@"email"]; 

謝謝大家的幫助。

0

我不會爲你寫代碼,但你需要看看如何實現UITableViewDataSource。特別是,你要做的是在tableView:cellForRowAtIndexPath:方法中。在互聯網上有成千上萬的好例子供你使用。

+0

我知道如何填充tableview,我不知道如何處理字典數組。 – rafarq