Detailed description of the table view grouping implementation code for iPhone development applications

Source: Internet
Author: User
Tags allkeys

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

 
 
  1. @ Interface SectionsViewController: UIViewController <UITableViewDataSource, UITableViewDelegate> {
  2. NSDictionary * names;
  3. NSArray * keys;
  4. }
  5. @ Property (nonatomic, retain) NSDictionary * names;
  6. @ Property (nonatomic, retain) NSArray * keys;
  7.  
  8. 4. Implementation Code
  9.  
  10. @ Implementation SectionsViewController
  11. @ Synthesize names;
  12. @ Synthesize keys;
  13.  
  14. -(Void) viewDidLoad {
  15. NSString * path = [[NSBundle mainBundle] pathForResource: @ "sortednames"
  16. OfType: @ "plist"]; // obtain the path of the attribute list and assign it to path
  17. NSDictionary * dict = [NSDictionary alloc]
  18. InitWithContentsOfFile: path]; // initialize the data table in the path to dict.
  19. Self. names = dict; // The dictionary dict is assigned to names.
  20.  
  21. // 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;
  22. [Dict release];
  23. // For (int I = 0; I <[[names allKeys] count]; I ++ ){
  24.  
  25. // NSLog (@ "% @ \ n", [[names allKeys] objectAtIndex: I]);
  26. //}
  27. //
  28. NSArray * array = [[names allKeys] sortedArrayUsingSelector:
  29. @ Selector (compare :)]; // sort all keys in alphabetical order
  30. // For (int I = 0; I <[array count]; I ++ ){
  31. // NSLog (@ "% @ \ n", [array objectAtIndex: I]);
  32. //}
  33.  
  34. Self. keys = array; // assign an array object to keys
  35.  
  36. }
  37.  
  38. -(Void) didReceiveMemoryWarning {
  39. // Releases the view if it doesn't have a superview.
  40. [Super didReceiveMemoryWarning];
  41. // Release any cached data, images, etc that aren't in use.
  42. }
  43. -(Void) viewDidUnload {
  44. // Release any retained subviews of the main view.
  45. // E.g. self. myOutlet = nil;
  46. Self. names = nil;
  47. Self. keys = nil;
  48. }
  49.  
  50. -(Void) dealloc {
  51. [Names release];
  52. [Keys release];
  53. [Super dealloc];
  54. }
  55.  
  56. # Pragma mark-
  57. # Pragma mark Table View Data Source Methods
  58. // Returns the number of partitions.
  59. -(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
  60. {
  61. Return [keys count]; // obtain the number of partitions.
  62. }
  63. // Returns the number of rows in each partition.
  64. -(NSInteger) tableView :( UITableView *) tableView
  65. NumberOfRowsInSection :( NSInteger) section
  66. {
  67. NSString * key = [keys objectAtIndex: section]; // The section is a partition and obtains the index of the section.
  68. NSArray * nameSection = [names objectForKey: key]; // obtain all data in the partition based on the index
  69. Return [nameSection count]; // return the number of rows in the partition.
  70. }
  71.  
  72. // Return the cell to be displayed, possibly to save memory
  73. -(UITableViewCell *) tableView :( UITableView *) tableView
  74. CellForRowAtIndexPath :( NSIndexPath *) indexPath
  75. {
  76. // NSLog (@ "tianshi \ n ");
  77. NSUInteger section = [indexPath section]; // returns the nth partition.
  78. NSUInteger row = [indexPath row]; // obtain the row number of the nth partition.
  79. NSString * key = [keys objectAtIndex: section]; // return the index key of the partition.
  80. NSArray * nameSection = [names objectForKey: key]; // return all content of the current partition obtained based on the key,
  81. Static NSString * SectionsTableIdentifier = @ "SectionsTableIdentifier ";
  82. // Determine whether the cell exists. If not, create a new one.
  83. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:
  84. SectionsTableIdentifier];
  85. If (cell = nil ){
  86. Cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
  87. ReuseIdentifier: SectionsTableIdentifier] autoreler];
  88. }
  89. // Assign values to cells
  90. Cell. textLabel. text = [nameSection objectAtIndex: row];
  91. Return cell;
  92. }
  93.  
  94. // Specify a name for each partition. The current name is the key value.
  95. -(NSString *) tableView :( UITableView *) tableView
  96. TitleForHeaderInSection :( NSInteger) section
  97. {
  98. NSString * key = [keys objectAtIndex: section];
  99. Return key;
  100. }
  101. // Add the index value for the A----E on the right
  102. -(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView
  103. {
  104. Return keys;
  105. }

Summary: DetailsIPhoneApplication Development tableViewThe content of grouping implementation code has been introduced. I hope this article will help you!

Related Article

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.