IOS learning notes-Table view 1 (create)

Source: Internet
Author: User

There is no limit on the number of rows in the IOS table. The number of rows is limited by the available storage space. The table has only one column. Is an instance of the UITableView class.

The table view does not store data in the table. Table view obtains configuration data from objects that follow the UITableViewDelegate protocol, and row data from objects that follow the UITableViewDataSource protocol. Each row in the table is represented by a UITableViewCell.

Follow the UITableViewDelegate protocol and UITableViewDataSource protocol in the. h file to declare the table view object and the data object in the table.

@ Interface LinViewController: UIViewController
 
  
// Create a table view object @ property (retain, nonatomic) UITableView * mTableView; // create an array object and store the table data @ property (retain, nonatomic) NSArray * array; @ end
 

Load the table view object and array object in the. m file, and implement the Protocol method selectively, as shown in the following example:

// Release the created object-(void) dealloc {[_ mTableView release]; [_ array release]; [super dealloc];}-(void) viewDidLoad {[super viewDidLoad]; // initializes the array and saves the font of the system as the data self in the table view. array = [UIFont familyNames]; // create the initialization table view self. mTableView = [[UITableView alloc] initWithFrame: self. view. frame style: UITableViewStylePlain]; // create a view as the header UIView * pView = [[UIView alloc] initWithFrame: CGRectMake (0, 0,320, 40)]; // set the View color pView. ba CkgroundColor = [UIColor blueColor]; // you can specify the header view self. mTableView. tableHeaderView = pView; // set the height of the header self. mTableView. sectionHeaderHeight = 50; // modify the line separator color self. mTableView. separatorColor = [UIColor redColor]; // set the split line type. Here, set it to disappear self. mTableView. separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched; // set the delegate object-Table delegate. Remember to add self. mTableView. dataSource = self; self. mTableView. delegate = self; // Add the table View Add to the current view [self. view addSubview: self. mTableView]; // release the created temporary variable [pView release];} # pragma mark ---- tableViewDataSource --- protocol method // set the number of rows in a segment ----- must implement-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [self. array count];} // draw each row of data in the table ----- must implement-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// set static variables, as the flag stat Ic NSString * identifer = @ "identifer"; // create a TableViewCell object and determine whether UITableViewCell * pCell = [tableView dequeueReusableCellWithIdentifier: identifer] already exists in the cache based on the static variable mark. // determine whether TableViewCell is null. if it is null, create a new if (nil = pCell) {pCell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: identifer];} // obtain the number of rows to be drawn. NSUInteger cellRow = [indexPath row]; // locate the NSString * pS Tring = [self. array objectAtIndex: cellRow]; // sets the pCell text in the row. textLabel. text = pString; // set the text font pCell in the line. textLabel. font = [UIFont fontWithName: pString size: 16]; // set the color of the text font in the line pCell. textLabel. textColor = [UIColor blueColor]; // set the comment of the text in the row. The pCell is displayed based on the format of TableViewCell. detailTextLabel. text = @ "detailText"; // you can specify the pCell icon in a row. imageView. image = [UIImage imageNamed: @ "text.png"]; // sets the flag format in the row. Add? PCell. accessoryType = UITableViewCellAccessoryCheckmark; return pCell;} // ----------- optional implementation method example // set text for the header-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {return @ "I'm Header_Title";} // indicates the set text. Generally,-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) is omitted) section {return @ "I'm Footer_Title";} # pragma mark ---- tableVi EwDelegate --- this method is optional when you select a row. The most common method is-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {// obtain the number of rows currently clicked NSUInteger row = [indexPath row]; // create a string to store the selected row information NSString * pString = [NSString stringWithFormat: @ "you selected row % I! ", Row]; // a warning box is displayed, showing the selected row UIAlertView * pAlert = [[UIAlertView alloc] initWithTitle: @" attention "message: pString delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; // display the warning box to release the created object [pAlert show]; [pAlert release];} // adjust the Row Height-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 44;} // adjust the header height-(CGFloat) tableView :( UITableView *) tableView heightForHeaderInSection :( NSInteger) section {return 20;} // offset of the row content-(NSInteger) tableView :( UITableView *) tableView indentationLevelForRowAtIndexPath :( NSIndexPath *) indexPath {// obtain the current row number, move the corresponding distance NSUInteger row = [indexPath row]; return row ;}




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.