IOS learning notes-Table View 4 (group partition)

Source: Internet
Author: User

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




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.