1. First effect to be achieved
Create a project based on sigle view application, drag a table View to the view, and implement outlets: datasource, delegate to file's owner.
Implementation Code:
# Import <uikit/uikit. h> // to fill the table, you must use one Protocol and implement two methods in the Protocol @ interface viewcontroller: uiviewcontroller <uitableviewdatasource> @ end
# Import "viewcontroller. H "@ implementation viewcontrollernsmutablearray * listofmovies; // set the information in the table. The row cells are in the index path-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindex *) indexpath {static nsstring * cellidentifier = @ "cell"; // set the battery uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier]; If (cell = nil) {Cell = [[[uitableviewcell alloc] initwithstyle: Invalid reuseidentifier: cellidentifier] autorelease];} // set the value of nsstring * cellvalue = [listofmovies objectatindex: indexpath. row]; cell. textlabel. TEXT = cellvalue; // Add image uiimage * image = [uiimage imagenamed: @ "ic_ic.jpg"]; cell. imageview. image = image; return cell;} // number of rows in the section-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return [listofmovies count];} -(nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) Section {// display the header return @ "movie list";}-(nsstring *) tableview :( uitableview *) tableview titleforfooterinsection :( nsinteger) Section {// display footer return @ "by Denzel Washington";} // select the index path line-(void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {// obtain the content of the selected row nsstring * movieselected = [listofmovies objectatindex: indexpath. row]; // encapsulate it into MSG nsstring * MSG = [nsstring stringwithformat: @ "you have selected % @", movieselected]; // use the warning box to display uialertview * Alert = [[uialertview alloc] initwithtitle: @ "movie selected" message: MSG delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; // The displayed dialog box [alert show]; // release alert [alert release];} // indent horizontally in the index path-(nsinteger) tableview :( uitableview *) tableview rows :( nsindexpath *) indexpath {return [indexpath row] % 2;} // specify the Row Height in the index path-(cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath {return 70;}-(void) viewdidload {listofmovies = [nsmutablearray alloc] init]; [listofmovies addobject: @ "Training Day"]; [listofmovies addobject: @ "Remember the Titans"]; [listofmovies addobject: @ "John Q. "]; [incluaddobject: @" The Bone Collector "]; [listofmovies addobject: @" risponet "]; [listofmovies addobject: @" The Siege "]; [incluaddobject: @ "Malcolm X"]; [listofmovies addobject: @ "Antwone Fisher"]; [listofmovies addobject: @ "courage under fire"]; [listofmovies addobject: @ "he got game"]; [listofmovies addobject: @ "The Pelican Brief"]; [listofmovies addobject: @ "Glory"]; [listofmovies addobject: @ "the preacher's wife"]; [Super viewdidload]; // do any additional setup after loading the view, typically from a nib .} -(void) dealloc {[listofmovies release]; [Super dealloc];}
I will only provide corresponding implementation methods!
2. Second Implementation Effect
Create a new application based on master-detail; create a file named movies. plist of the property list type in the file. The content is as follows:
Implementation Code:
#import <UIKit/UIKit.h>@class DetailViewController;@interface MasterViewController : UITableViewController{ NSDictionary *movieTitles; NSArray *years;}@property (nonatomic,retain)NSDictionary *movieTitles;@property (nonatomic,retain)NSArray *years;@property (strong, nonatomic) DetailViewController *detailViewController;@end
# Import "masterviewcontroller. H "# import" detailviewcontroller. H "@ implementation masterviewcontroller @ synthesize movietitles, years;-(void) dealloc {[_ detailviewcontroller release]; [movietitles release]; [years release]; [Super dealloc];} -(void) viewdidload {// file name and type nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "Movies" oftype: @ "plist"]; // The retrieved content is of the dictionary type nsdictionary * DIC = [nsdictionary alloc] I Nitwithcontentsoffile: path]; // assign all content to movietitles self. movietitles = DIC; [DIC release];/* obtain all the years, and the ascending key is 2000,200 1, 2002,200 4, 2006,200 7, 2008 */nsarray * array = [self. movietitles allkeys] sortedarrayusingselector: @ selector (compare :)]; // assigned to the array year self. years = array; [Super viewdidload]; // do any additional setup after loading the view, typically from a nib .} -(nsinteger) numberofsectionsintableview :( uitab Leview *) tableview {// The total number of returned rows return [self. years count];} // The number of rows per section-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {// obtain the nsstring * year = [self. years objectatindex: section]; // obtain the value of each year and obtain an array nsarray * moviesection = [self. movietitles objectforkey: year]; // return the total value of this key. Return [moviesection count];} // Add the content of each section-(uitableviewcell *) tableview :( uitableview *) tableview Cell Forrowatindexpath :( nsindexpath *) indexpath {static nsstring * cellidentifier = @ "cell"; uitableviewcell * cell = [tableview failed: cellidentifier]; If (cell = nil) {Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier] autoreler];} // obtain the nsstring * year = [self. years objectatindex: [indexpath section]; // obtain the value of nsarray * mov per year Iesection = [self. movietitles objectforkey: year]; // sets the content cell in each section. textlabel. TEXT = [moviesection objectatindex: [indexpath row]; return cell;} // year header-(nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) section {nsstring * year = [self. years objectatindex: section]; return year;}-(void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {If (! Self. detailviewcontroller) {self. detailviewcontroller = [[[detailviewcontroller alloc] initwithnibname: @ "detailviewcontroller" Bundle: Nil] autorelease];} [self. navigationcontroller pushviewcontroller: Self. detailviewcontroller animated: Yes];}
Open the masterviewcontroller. XIB file, change the style attribute of table view to grouped, and add an index in masterviewcontroller. m using the following code:
// Sometimes the list is too long. Add this method to implement the index. index every year-(nsarray *) sectionindextitlesfortableview :( uitableview *) tableview {return years ;}
The effects are as follows:
Next we switch to another program and bring the movie name back:
Add the following code to the detailviewcontroller. M file:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *year = [self.years objectAtIndex:[indexPath section]]; NSArray *movieSection = [self.movieTitles objectForKey:year]; NSString *movieTitle = [movieSection objectAtIndex:[indexPath row]]; NSString *message = [[NSString alloc]initWithFormat:@"%@", movieTitle]; if (!self.detailViewController) { self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease]; } self.detailViewController.movieSelected=message; [self.navigationController pushViewController:self.detailViewController animated:YES];}
Add a label to the detailviewcontroller. XIB file;
Add the following information to the detailviewcontroller. h file:
# Import <uikit/uikit. h> @ interface detailviewcontroller: uiviewcontroller {nsstring * movieselected; // the name of the movie iboutlet uilabel * label;} @ property (strong, nonatomic) ID detailitem; @ property (strong, nonatomic) iboutlet uilabel * detaildescriptionlabel; @ property (nonatomic, retain) nsstring * movieselected; @ property (nonatomic, retain) iboutlet uilabel * label; @ end
Add the following in the detailviewcontroller. M file:
@interface DetailViewController ()- (void)configureView;@end@implementation DetailViewController@synthesize detailItem = _detailItem;@synthesize detailDescriptionLabel = _detailDescriptionLabel;@synthesize movieSelected,label;- (void)dealloc{ [_detailItem release]; [_detailDescriptionLabel release]; [movieSelected release]; [super dealloc];}- (void)viewDidLoad{ self.navigationItem.title = movieSelected; label.text=movieSelected; [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. [self configureView];}
Implementation:
This blog is my own exercise. I have not explained many things clearly. Please forgive me!