填充一個的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必須顯示名稱的主標籤和電子郵件的副標題。
在此先感謝。
你顯示的JSON是一個字典數組......你有什麼具體問題? – Wain
這確實是一個基本問題。鑑於你已經意識到你有一個'NSArray'的NSDictionary'對象(正如Wain指出的那樣),它非常適合表格視圖數據源,你可以在這裏找到答案:[Table View Programming Guide for iOS](https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html),尤其是章節「仔細觀察表格視圖單元格」。 – CouchDeveloper
對不起,提出這樣一個基本問題,但我找不到如何從字典中獲取nsarray,我總是將一個「[__NSArrayM objectForKey:]:無法識別的選擇器發送到實例」我真的不知道如何處理與此同時,我一直在閱讀所有的網絡兩天。 – rafarq