Groups and partitions the table view to facilitate information search. First, you must create a. plist file that contains all the information to facilitate data entry during table view loading. Use dictionary storage to obtain table View data in arrays. Partition the table view according to the UITableViewDelegate protocol.
First, add and write the. plist file, write data, and import the file:
Add the protocol to the. h file and create the object:
@ Interface LinViewController: UIViewController
// Create a table view object @ property (retain, nonatomic) UITableView * mTableView; // create a dictionary object @ property (retain, nonatomic) NSDictionary * mDictionary; // create an array object @ property (retain, nonatomic) NSArray * mArray; @ end
Add the implementation method to the. m file:
@ Implementation LinViewController // release the created object-(void) dealloc {[_ mTableView release]; [_ mDictionary release]; [_ mArray release]; [super dealloc];} // load view-(void) viewDidLoad {[super viewDidLoad]; // create the initialization table view self. mTableView = [[UITableView alloc] initWithFrame: self. view. frame style: UITableViewStyleGrouped]; // sets the delegate object self. mTableView. dataSource = self; self. mTableView. delegate = self; // load the table View to the current view. view addSubview: self. mTableView]; // get. path of the plist file NSString * path = [[NSBundle mainBundle] pathForResource: @ "data" ofType: @ "plist"]; // initialize the dictionary self. mDictionary = [NSDictionary dictionaryWithContentsOfFile: path]; // initialize the array and obtain the data self based on the dictionary object. mArray = [[self. mDictionary allKeys] sortedArrayUsingSelector: @ selector (compare :)]; // The associated method here is the display method of arrangement, in this case, the data is alphabetically arranged} # pragma mark --- UITableViewDataSource --------- // obtain the number of rows in the table view based on the array element-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [[self. mDictionary objectForKey: [self. mArray objectAtIndex: section] count];} // draw each row of table-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// set a static string as a tag (static prevents multiple creation of local variables, improving efficiency and saving memory) static NSString * identifer = @ "identifer "; // create a cell object (using the mechanism that indicates that the image can be reused) UITableViewCell * pCell = [tableView dequeueReusableCellWithIdentifier: identifer]; // if the cell is empty, create a new if (nil = pCell) {pCell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: identifer];} // obtain the region NSInteger section = [indexPath section]; // obtain the number of rows in the current table view NSInteger row = [indexPath row]; // pass the corresponding elements in the array to the view's text pCell. textLabel. text = [[self. mDictionary objectForKey: [self. mArray objectAtIndex: section] objectAtIndex: row]; return pCell;} // display the header title of the region-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {return [self. mArray objectAtIndex: section];} // index of the display area-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {return self. mArray ;}# pragma mark --- UITableViewDelegate ----------- // Partition Table view-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return [self. mArray count];}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning];} end