Objective-c ui UITableView details, objectivecui

Source: Internet
Author: User

Objective-c ui UITableView details, objectivecui

UITableView plays an important role in IOS development and must be mastered.

Before learning about UITableView, let's take a look at some basic concepts:

  • UITableView inherits from UIScrollView and is a control that supports vertical scrolling.
  • The Cell corresponding to each piece of data in UITableView is called Cell. It is an object of UITableViewCell and inherits from UIView.
  • UITableView can be displayed in partitions. Each partition is called a section and each row is called a row. The number starts from 0.
  • The system provides a class to integrate section and row, called NSIndexPath.

As you can see above, section and row represent the position of a UITableViewCell on UITableView.

Next, we create a UITableView:

// Style is a UITableViewStyle parameter and an enumeration type, including UITableViewStylePlain and UITableViewStyleGrouped
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];[self.view addSubview:tableView];

The following are common attributes of UITableView:

RowHeight Row Height
SeparatorStyle Separator Style
SeparatorColor Separator color
TableHeaderView Top view of UITableView
TableFooterView UITableView bottom view

 

 

 

 

 

UITableView Basics

UITableView has two important attributes: dataSource (following the UITableViewDataSource Protocol) and delegate (following the UITableViewDelegate protocol)

DataSource is a proxy related to display data, and delegate is a proxy related to view operations.

The UITableViewDataSource protocol has two required protocol methods:

-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section;

UITableView the number of rows contained in each partition

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath;

Cell to be displayed for each row

 

 

 

 

The first method returns different rows based on the given parameter section.

Method 2: tableView calls this method every time a Cell is displayed.

 

Each cell of UITableView is an object of the UITableViewCell class. By default, three view attributes are provided:

The following is an example of returning Cell: (if registerClass is not used for registration)

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * cellID = @ "cell"; // The identifier, find available cells in the reuse pool of tableView (a variable set in the reuse pool) UITableViewCell * Cell = [tableView dequeueReusableCellWithIdentifier: cellID]; // if the cell corresponding to this identifier does not exist in the reuse pool, a new cell is created and the Identifier if (! Cell) {// only process the Cell style in the code block, and do not set cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue2 reuseIdentifier: cellID];} // set the Cell data [cell. textLabel setText: @ "title"]; [cell. detailTextLabel setText: @ "Description: This is the title"]; return cell ;}

UITableView has a reuse pool mechanism to manage cells. The goal is to use as few cells as possible to display all data.

UITableView reuse Cell Process

After creating UITableView, You need to register a Cell class. When there is no Cell in the reuse pool, the system can automatically create a Cell. Related methods:

[TableView registerClass :( Class) cellClass forCellReuseIdentifier :( NSString *) identifier]; (you can use different identifier for multiple registration)

The system provides a method to obtain the Cell in the reuse pool (A Reuse ID is required ):

-(UITableViewCell *) dequeueReusableCellWithIdentifier :( NSString *) identifier;

 

Common protocol methods of UITableView

  • UITableViewDataSource

-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView

Number of UITableView partitions

-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section

Partition top title

-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) section

Bottom title of the partition

-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView

Index on the Right of UITableView

 

 

 

 

 

 

 

 

  • UITableViewDelegate

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath

Tell delegate that a Cell is selected

-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath

Height of each row

-(CGFloat) tableView :( UITableView *) tableView heightForHeaderInSection :( NSInteger) section

Top height of each partition

-(UIView *) tableView :( UITableView *) tableView viewForHeaderInSection :( NSInteger) section

Custom view at the top of each partition

 

 

 

 

 

 

 

 

 

UITableView editing

Process:

Note: After editing, the numberOfRowInSection protocol method is called only once when tableView is added to the parent view, and the data on the table is provided by an array. Therefore, first, you need to change the data in the array, and then let the table protocol re-call to re-assign values.

That is, first modify the data source and refresh the table (use the [table reloadData] method)

 

UITableView mobile

 

UITableViewCell

1. Custom Cell

Generally, the frame size of the Cell is (,) when it is created, and the height of the Cell we set is usually greater than 44. Therefore, the frame for creating a subview in a custom Cell is CGRectZero. When the Cell is added to tableView, the frame is set for the sub-view. when the Cell is added to tableView, the size has been changed to the size set by tableView, therefore, in the Custom Cell Method layoutSubviews, set the frame of the subview.

2. Use of Model

The role of the Model class is to provide us with data. Generally, our data is stored in arrays and dictionaries. The KVC in OC helps us to convert the dictionary to the Model class.

Procedure:

Note: The Model class needs to override-(void) setValue :( id) value forUndefinedKey :( NSString *) key to prevent crash when the attribute with the same key value cannot be found, when the key value is a system keyword, you can assign a value to the corresponding attribute (the attribute name does not conflict with the system keyword) in the method, such as _ id = value;

3. Mixed use of multiple cells

Different cells need to use different reusable identifiers for differentiation, while the reuse identifiers need to be differentiated based on different situations, such:

  • Model attribute differentiation (different data content, such as the type field in the data, 0 indicates the text type, and 1 indicates the image type)
  • Fixed Rows display different Cell types

4. Adaptive height

 

 

Reprinted Please note: Author SmithJackyson

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.