The iPhone uses JSON to transmit data, which is displayed on the table interface.

Source: Internet
Author: User

The JSON data is used above. Although the iPhone SDK does not support the JSON data, it has been written by a third party.
JSON reference: http://code.google.com/p/json-framework/
 
The specific code implementation is as follows:
Data Loading:
# Import "mydatasource. h"
# Import "JSON. H"
@ Implementation mydatasource
+ (Nsdictionary *) fetchlibraryinformation
{
Nsstring * urlstring = [nsstring stringwithformat: @ "http://wangjun.easymorse.com/wp-content/video/hello.jison"];
Nsurl * url = [nsurl urlwithstring: urlstring];
Nslog (@ "fetching library data ");
Return [self fetchjsonvalueforurl: url];
}
+ (ID) fetchjsonvalueforurl :( nsurl *) URL
{
Nsstring * jsonstring = [[nsstring alloc] initwithcontentsofurl: URL
Encoding: nsutf8stringencoding error: Nil];
 
Id jsonvalue = [jsonstring jsonvalue];
[Jsonstring release];
Return jsonvalue;
}
@ End
Table data display:
# Import "jsontabletestviewcontroller. H"
# Import "mydatasource. h"
@ Implementation jsontabletestviewcontroller
@ Synthesize mydata;
-(Void) viewdidload {
Nslog (@ "load data");
Mydata = [[mydatasource fetchlibraryinformation] retain];
}
-(Void) didreceivememorywarning {
// Releases the view if it doesn't have a superview.
[Super didreceivememorywarning];
 
// Release any cached data, images, etc that aren't in use.
}
-(Void) viewdidunload {
// Release any retained subviews of the main view.
// E.g. Self. myoutlet = nil;
}
-(Nsinteger) numberofsectionsintableview :( uitableview *) tableview {
Return [mydata count]; // The number of sections, that is, "several"
}
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {
Return [[mydata valueforkey: [[mydata allkeys] objectatindex: section] Count];
// Here, we need to tell uitableviewcontroller that there are several in each section, that is, "there are several people in one"
}
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {
 
Static nsstring * cellidentifier = @ "cell ";
Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier];
If (cell = nil ){
Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault
Reuseidentifier: cellidentifier] autoreler];
}
// The above items are all repeated and white-handed. You don't have to think about why, just copy them.
Cell. textlabel. Text = [[mydata valueforkey: [[mydata allkeys] objectatindex: indexpath. Section] objectatindex: indexpath. Row];
// This sentence looks complicated, but it is actually finding the corresponding array in a specific section,
// Find the content of indexpath. Row in array
Return cell;
}
-(Nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) Section
{
Return [[mydata allkeys] objectatindex: section];
// Set the name of the corresponding section here. allkey returns all the key values as an array, that is, "Zhang jia" and "Li Jia"
// Use objectatindex: to find out which one it is!
}
-(Void) dealloc {
[Mydata release];
[Super dealloc];
}
@ End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.