IOS Study Notes (12): Related Classes of UITableView in IOS development, and implementation of attributes and table view (2)

Source: Internet
Author: User

IOS study note (12) IOS development of the table view (UITableView) Explanation and use (2) (blog address: http://blog.csdn.net/developer_jiangqq) reprinted please note the address.

Author: hmjiangqq

Email: jiangqqlmj@163.com

In the previous article, I first learned about the basic concepts of the table view (UITableView) (Click to enter). Today I will learn other knowledge about the table view and implement the table view.

(1) Analysis of UITableView related classes:

First, let's look at the structure of the class:

1: The table view (UITableView) is inherited from UIScrollView, so that our table view can be rolled up or down.

2: at the same time table view (UITableView), there are two delegates ①: UITableViewDelegate delegation protocol, which is generally used to process the basic style of the table view (such as the cell height) you can also capture the selected cell events. ②: The UITableViewDataSource delegation protocol. It is necessary to implement the data source method of this Protocol to complete the data configuration of the table view.

3: UITableViewController: the Controller class of the table view (UITableView.

4: UItableViewCell: cell class.


(2) Data Source protocol and delegated source protocol:

1: UITableViewDataSource Protocol: we implement the method to complete the data configuration of our table view to display the table view. The following two methods must be implemented:

// Return the number of cells in each section-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section; // Row display. implementers shocould * always * try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with outputs: // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) // create data for cells in the table view-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath;
In addition to the two required implementation methods, there are also some optional implementation methods:

// Return the number of sections-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView; // Default is 1 if not implemented // return section (section) header title-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section; // fixed font style. use custom view (UILabel) if you want something different // return the title at the end of section (section)-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) section; (BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath;-(BOOL) tableView :( UITableView *) tableView canMoveRowAtIndexPath :( NSIndexPath *) indexPath;-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView; // return list of section titles to display in section index view (e.g. "ABCD... Z # ")-(NSInteger) tableView :( UITableView *) tableView sectionForSectionIndexTitle :( NSString *) title atIndex :( NSInteger) index;-(void) tableView :( UITableView *) tableView commitEditingStyle :( partial) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath;-(void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath toIndexPath :( NSIndexPath *) destinationIndexPath;
2: UITableViewDelegate: the Protocol can be used to set the beginning and end of a node in the table view and to respond to click events. The main methods are as follows:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
For more methods, go to UITableView on the official website.
(3) Some common methods and attributes of table view (UITableView:

1: common attributes:

①: @ Property (nonatomic) UITableViewCellSeparatorStyle separatorStyle; default value: UITableViewCellSeparatorStyleSingleLine

②: @ Property (nonatomic, retain) UIColor * separatorColor; default value: the standard separator gray

③: @ Property (nonatomic, retain) UIView * tableHeaderView; header View

④: @ Property (nonatomic, retain) UIView * tableFooterView; tail View

⑤: @ Property (nonatomic) CGFloat rowHeight; // cell height

⑥: @ Property (nonatomic) CGFloat sectionHeaderHeight; // header row height

7: @ property (nonatomic) CGFloat sectionFooterHeight; // tail Row Height

Usage: @ property (nonatomic, readwrite, retain) UIView * backgroundViewNS_AVAILABLE_IOS (3_2 );

Usage: @ property (nonatomic, readonly) UITableViewStyle style;

2: common methods:

①:-(Void) reloadData; // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks refreshes cell data

②:-(Void) reloadSectionIndexTitlesNS_AVAILABLE_IOS (3_0); // reloads the index bar.

③:-(NSInteger) numberOfSections; // number of returned sections

④:-(NSInteger) numberOfRowsInSection :( NSInteger) section; // returns the number of cells in each section.

⑤:-(CGRect) rectForSection :( NSInteger) section; // includes des header, footer and all rows

⑥:-(CGRect) rectForHeaderInSection :( NSInteger) section;

7:-(CGRect) rectForFooterInSection :( NSInteger) section;

Direction:-(CGRect) rectForRowAtIndexPath :( NSIndexPath *) indexPath;

Usage:-(NSIndexPath *) indexPathForRowAtPoint :( CGPoint) point; // returns nil if point is outside table

RESPONSE:-(NSIndexPath *) indexPathForCell :( UITableViewCell *) cell; // return the NSIndexPath instance of the specified cell

11:-(NSArray *) indexPathsForRowsInRect :( CGRect) rect; // return the NSIndexPath instance array in the specified range

12:-(UITableViewCell *) cellForRowAtIndexPath :( NSIndexPath *) indexPath; // returns nil if cell is not visible or index path is out of range // return the cell instance of the specified NSIndexPath instance

Thirteen:-(NSArray *) visibleCells; // returns an array of visible Cells

Fourteen-(NSArray *) indexPathsForVisibleRows; // returns the NSIndexPath instance array of the visible cell

15th:-(UITableViewHeaderFooterView *) headerViewForSection :( NSInteger) sectionNS_AVAILABLE_IOS (6_0 );

16:-(UITableViewHeaderFooterView *) footerViewForSection :( NSInteger) sectionNS_AVAILABLE_IOS (6_0 );

17:-(void) scrollToRowAtIndexPath :( NSIndexPath *) indexPath atScrollPosition :( UITableViewScrollPosition) scrollPosition animated :( BOOL) animated; // slide to the specified position and add the animation effect

18:-(void) scrollToNearestSelectedRowAtScrollPosition :( UITableViewScrollPosition) scrollPosition animated :( BOOL) animated;


(4) Example of table layout

To put it simply, follow these steps: 1. Configure the data source, 2. Implement the data source method, and 3. Set the proxy method. Next let's look at the instance

/// ZTTRootViewController. m // UITableViewDemo1 /// Created by Jiang qingqing on 14-3-19. // Copyright (c) 2014 Jiang qingqing
 
  
. All rights reserved. // # import "ZTTRootViewController. h "# import" ZTTDetailsViewController. h "# define kDeviceHeight [UIScreen mainScreen]. bounds. size. height @ interface ZTTRootViewController () @ end @ implementation ZTTRootViewController-(id) initWithNibName :( NSString *) bundle :( NSBundle *) handle {self = [super initWithNibName: bundle: role]; if (self) {self. titl E = @ "UITableView Style";} return self;}-(void) loadView {UIView * view = [[UIView alloc] initWithFrame: [UIScreen mainScreen]. applicationFrame]; // [view setBackgroundColor: [UIColor redColor]; self. view = view; [view release]; // start to configure the data source self. listArray = @ [@ "UITableViewStylePlain", @ "UITableViewStyleGrouped"]; _ tableView = [[UITableView alloc] initWithFrame: CGRectMake (0, 0,320, kDeviceHeight-20-44) style: UITab LeViewStylePlain]; // method for implementing the data source [_ tableView setDataSource: self]; // set the Click Event proxy method [_ tableView setDelegate: self]; [self. view addSubview: _ tableView];}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view .} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} # pragma mark-tableview date source/** a select Number of cells in ion */-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [_ listArray count];} // indexPath // create a cell-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * cellInditifier = nil; // create a cell object UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellInditifier]; if (cell = nil) {Cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: cellInditifier] autorelease];} NSString * text = [self. listArray objectAtIndex: indexPath. row]; cell. textLabel. text = text; return cell;} // Several selection-(NSInteger) numberOfSectionsInTableView (UITableView *) tableView {return 1;} // method of the selected cell-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIn DexPath *) indexPath {NSLog (@ "didSelect"); // jump to the corresponding page ZTTDetailsViewController * detailsVC = [[ZTTDetailsViewController alloc] init]; detailsVC. isPlain = indexPath. row = 0? YES: NO; [self. navigationController pushViewController: detailsVC animated: YES]; [detailsVC release];}-(void) dealloc {[_ tableView release]; _ tableView = nil; [super dealloc];} @ end
 
Run the following command:


3: the code example above is a general table. If we want to add header and footer to the table, we need to implement the following two data source methods:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;">-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section; // fixed font style. use custom view (UILabel) if you want something different-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) section;Run the following command:




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.