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