IPhoneDevelop an application tableViewThe implementation of grouping is the content to be introduced in this article, mainly based on code implementationViewGroup, not to mention, first look at the details of this article, paste a picture:
1. Create a plist file first,
2. Place a table view control on the main interface.
3. interface code
- @ Interface SectionsViewController: UIViewController <UITableViewDataSource, UITableViewDelegate> {
- NSDictionary * names;
- NSArray * keys;
- }
- @ Property (nonatomic, retain) NSDictionary * names;
- @ Property (nonatomic, retain) NSArray * keys;
-
- 4. Implementation Code
-
- @ Implementation SectionsViewController
- @ Synthesize names;
- @ Synthesize keys;
-
- -(Void) viewDidLoad {
- NSString * path = [[NSBundle mainBundle] pathForResource: @ "sortednames"
- OfType: @ "plist"]; // obtain the path of the attribute list and assign it to path
- NSDictionary * dict = [NSDictionary alloc]
- InitWithContentsOfFile: path]; // initialize the data table in the path to dict.
- Self. names = dict; // The dictionary dict is assigned to names.
-
- // Because names has the retain attribute. When a names value is assigned, dict automatically adds a retain). At this time, dict's retain count = 2;
- [Dict release];
- // For (int I = 0; I <[[names allKeys] count]; I ++ ){
-
- // NSLog (@ "% @ \ n", [[names allKeys] objectAtIndex: I]);
- //}
- //
- NSArray * array = [[names allKeys] sortedArrayUsingSelector:
- @ Selector (compare :)]; // sort all keys in alphabetical order
- // For (int I = 0; I <[array count]; I ++ ){
- // NSLog (@ "% @ \ n", [array objectAtIndex: I]);
- //}
-
- Self. keys = array; // assign an array object to keys
-
- }
-
- -(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;
- Self. names = nil;
- Self. keys = nil;
- }
-
- -(Void) dealloc {
- [Names release];
- [Keys release];
- [Super dealloc];
- }
-
- # Pragma mark-
- # Pragma mark Table View Data Source Methods
- // Returns the number of partitions.
- -(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
- {
- Return [keys count]; // obtain the number of partitions.
- }
- // Returns the number of rows in each partition.
- -(NSInteger) tableView :( UITableView *) tableView
- NumberOfRowsInSection :( NSInteger) section
- {
- NSString * key = [keys objectAtIndex: section]; // The section is a partition and obtains the index of the section.
- NSArray * nameSection = [names objectForKey: key]; // obtain all data in the partition based on the index
- Return [nameSection count]; // return the number of rows in the partition.
- }
-
- // Return the cell to be displayed, possibly to save memory
- -(UITableViewCell *) tableView :( UITableView *) tableView
- CellForRowAtIndexPath :( NSIndexPath *) indexPath
- {
- // NSLog (@ "tianshi \ n ");
- NSUInteger section = [indexPath section]; // returns the nth partition.
- NSUInteger row = [indexPath row]; // obtain the row number of the nth partition.
- NSString * key = [keys objectAtIndex: section]; // return the index key of the partition.
- NSArray * nameSection = [names objectForKey: key]; // return all content of the current partition obtained based on the key,
- Static NSString * SectionsTableIdentifier = @ "SectionsTableIdentifier ";
- // Determine whether the cell exists. If not, create a new one.
- UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:
- SectionsTableIdentifier];
- If (cell = nil ){
- Cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
- ReuseIdentifier: SectionsTableIdentifier] autoreler];
- }
- // Assign values to cells
- Cell. textLabel. text = [nameSection objectAtIndex: row];
- Return cell;
- }
-
- // Specify a name for each partition. The current name is the key value.
- -(NSString *) tableView :( UITableView *) tableView
- TitleForHeaderInSection :( NSInteger) section
- {
- NSString * key = [keys objectAtIndex: section];
- Return key;
- }
- // Add the index value for the A----E on the right
- -(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView
- {
- Return keys;
- }
Summary: DetailsIPhoneApplication Development tableViewThe content of grouping implementation code has been introduced. I hope this article will help you!