UITabelView proxy Methods

Source: Internet
Author: User

UITabelView proxy Methods

Click to open the download link
//// RootViewController. m // LessonUITabelView /// Created by lanouhn on 14-9-3. // Copyright (c) 2014 vaercly@163.com Chen conglei. all rights reserved. // # import "RootViewController. h "# import" StudentCell. h "@ interface RootViewController ()
 
  
// When storing data with a one-to-one correspondence relationship, use a dictionary to store @ property (nonatomic, retain) NSDictionary * names; @ property (nonatomic, retain) NSArray * titles; // storage partition title @ end @ implementation RootViewController-(id) initWithNibName :( NSString *) bundle :( NSBundle *) handle {self = [super initWithNibName: nibNameOrNil bundle: role]; if (self) {// Custom initialization NSArray * firstGroup = @ [@ "Cong lei", @ "Xiao Ming", @ "puppy"]; NS Array * secondGroup = @ [@ "Cong", @ "kitten", @ "Xiao Tian", @ "Fei"]; NSArray * thirdGroup = @ [@ "", @ "Liang", @ "aa", @ "bb", @ "Xiao Guang", @ "Xiao Ming"]; self. names = @ {@ "A": firstGroup, @ "D": secondGroup, @ "C": thirdGroup}; // retrieve the keys in the dictionary and sort themselves in ascending order. titles = [[self. names allKeys] sortedArrayUsingSelector: @ selector (compare :)];} // load data during initialization [self readDataFromPlist]; return self;}-(void) readDataFromPlist {// 1 obtain the file path NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "Student" ofType: @ "plist"]; // 2 initialize the dictionary object NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile: filePath]; NSLog (@ "% @", dic);}-(void) loadView {// tabelView table view, it is a control used in iOS to display and edit a series of information lists with the same data structure. UITabelView inherits from UIScrollView, so it can slide, but it can only slide vertically and has only one column, UITabelView is a partition (section, which corresponds to the group of class students) and a column (row, which corresponds to members of a group). The index values for partitions and rows start from 0, if you want to obtain a row (get a class Is determined based on the row of the partition (based on the student's group and the position in the group (the number of persons, both information is stored in the NSIndexPath Class Object. each row of UITabelView is a UITabelViewCell object. Each cell contains imageView (left), label (textLabel, detailLabel, center), accessoryView (right) UITableView * tabelView = [[UITableView alloc] initWithFrame: [UIScreen mainScreen]. bounds style: UITableViewStylePlain]; // sets the tabelView of the split line. separatorStyle = UITableViewCellSeparatorStyleSingleLine; t AbelView. separatorColor = [UIColor grayColor]; // specify the tabelView data source to provide data support for tabelView display. It must comply with the tabelView protocol of UITabelViewDataSource. dataSource = self; // sets the tableView proxy (to handle cell click events) tabelView. delegate = self; // set the height of the tableView row to tabelView. rowHeight = 70; self. view = tabelView; [tabelView release];}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. self. navigationItem. t Itle = @ "9";} // set the number of rows of tabelView (the number of rows in each group)-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// 1 first obtain the key value // NSString * key = self. titles [section]; // 2. obtain the corresponding array based on the key value. // NSArray * group = self. names [key]; // 3. Calculate the number of elements in the array // return group. count; // return [self. names [self. titles [section] count]; return 1000;} // used to create a cell. Each row must correspond to a cell-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil]; /// 1 first obtain the key // NSString * key = self. titles [indexPath. section]; // 2 obtain value (array) based on the key // NSArray * group = self. names [key]; // 3 remove elements from the array // cell. textLabel. text = group [indexPath. row]; // cell. textLabel. text = self. Names [self. titles [indexPath. section] [indexPath. row]; // cell. textLabel. text = [[self. names objectForKey: [self. titles objectAtIndex: indexPath. section] objectAtIndex: indexPath. row]; // 1 create a reuse identifier static NSString * identifier = @ "lanou7ban "; // 2 reuse the queue based on the identifier to retrieve the reusable cell StudentCell * cell = [tableView dequeueReusableCellWithIdentifier: identifier]; // 3 determine whether the reusable cell if (! Cell) {// If the reusable cell is not obtained successfully, create a cell. // after the cell is created, you must add a reuse identifier to the cell, facilitate reuse of cell = [[[StudentCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: identifier] autorelease];} cell. textLabel. text = @ "vaercly"; cell. detailTextLabel. text = @ "frank"; return cell;} # pragma mark-UITableViewDelegate // sets the Row Height-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {// The cell height of the even number is 100 // The cell height of the odd number is 50; if (indexPath. row % 2) {return 100;} return 50;} // when the cell is selected, it is triggered-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "% d", indexPath. row) ;}# pragma mark-uitableviewsponse/* // returns the number of tableView partitions-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return self. names. count;} // set the text displayed in each partition header-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {return self. titles [section];} // set the index value on the left of tableView (used to quickly locate the partition for easy search). It must correspond to the title of each partition-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {return self. titles;}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} */-(void) dealloc {self. names = nil; [super dealloc];}/* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller .} * // @ end
 

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.