Knowledge about tableView in IOS

Source: Internet
Author: User

Knowledge about tableView in IOS

  • Introduction

  • Published using GitBook tableView Performance Optimization-cell Recycling Method 1tableView Performance Optimization-cell Recycling method 1
    /*** When to call: Whenever a cell enters the field of view, it will call */-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// 0. reuse the identifier // local variable modified by static: It is initialized only once. During the entire program running, there is only one static NSString * ID = @ "cell"; // 1. find the reusable cell UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: ID] in the cache pool based on the cell ID. // 2. if the cell is nil (the corresponding cell cannot be found in the cache pool) if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: ID];} // 3. overwrite the data cell. textLabel. text = [NSString stringWithFormat: @ "testdata-% zd", indexPath. row]; return cell ;}
    TableView Performance Optimization-cell Recycling method 2 define a global variable
    // Define the reuse ID NSString * ID = @ "cell ";
    Register the cell type corresponding to an ID
    // Register cell-(void) viewDidLoad {[super viewDidLoad] In this method; // register the cell type [self. tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: ID];}
    Return cell in Data Source Method
    -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// 1. find cell UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: ID] in the cache pool; // 2. overwrite the data cell. textLabel. text = [NSString stringWithFormat: @ "testdata-% zd", indexPath. row]; return cell ;}
    TableView Performance Optimization-cell Recycling method 3

    Set the Dynamic Prototypes Cell of UITableView in storyboard

    Set cell reuse ID

    Retrieve cell with reuse ID in code

    // 0. reuse the identifier // local variable modified by static: It is initialized only once. During the entire program running, there is only one static NSString * ID = @ "cell"; // 1. search for cyclically available cellUITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: ID] According to the cell ID. // 2. overwrite the data cell. textLabel. text = [NSString stringWithFormat: @ "cell-% zd", indexPath. row]; return cell;
    Use UIViewController as UITableViewController.

    Common UITableView settings
    // Split line color self. tableView. separatorColor = [UIColor redColor]; // hide the split line self. tableView. separatorStyle = UITableViewCellSeparatorStyleNone; // when tableView has data, you need to split the line. // development tips: quickly cancel the split line self. tableView. tableFooterView = [[UIView alloc] init];
    Common UITableViewCell settings
    // Cancel the selected style (commonly used) so that no response cell is pressed for the current cell. selectionStyle = UITableViewCellSelectionStyleNone; // set the selected background color UIView * selectedBackgroundView = [[UIView alloc] init]; selectedBackgroundView. backgroundColor = [UIColor redColor]; cell. selectedBackgroundView = selectedBackgroundView; // sets the Default background color cell. backgroundColor = [UIColor blueColor]; // sets the Default background color. UIView * backgroundView = [[UIView alloc] init]; backgroundView. backgroundColor = [UIColor greenColor]; cell. backgroundView = backgroundView; // priority of backgroundView> backgroundColor // set indicator // cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell. accessoryView = [[UISwitch alloc] init];
    Custom cell

    High cell

    Storyboard custom cell

    1. Create a subclass that inherits from UITableViewCell, such as XMGDealCell
    2. Add the child control to the cell in the storyboard.
    Set cell reuse ID
    Set the cell class to XMGDealCell.
    3. Use the reuse identifier in the Controller to find the cell and transmit model data to the cell.
    4. In XMGDealCell, connect the child control in the storyboard to the class extension.
    You must provide a model attribute to override the set Method of the model. In this method, set the model data to the Child control.

    Xib custom cell

    1. Create a subclass that inherits from UITableViewCell, such as XMGDealCell
    2. Create an xib file (the file name is recommended to be the same as the cell class name). For example, XMGDealCell. xib drag a UITableViewCell to change the cell class to XMGDealCell.Set cell reuse IDAdd the child control to the cell. 3. Add the child control to the Controller.Use registerNib... to register the xib FileFind the cell by using the re-identification (if the xib file is not registered, You need to manually load the xib file) and transmit model data to the cell.
    4. connecting child controls in xib to class extension in XMGDealCell requires a model attribute to override the set Method of the model, in this method, you can set the model data to the Child control and encapsulate the code for creating and obtaining the cell (for example, cellWithTableView: method)

    Code custom cell (using frame)

    1. create a subclass that inherits from UITableViewCell. For example, XMGDealCell adds a child control to initWithStyle: reuseIdentifier: method to set the initialization attributes of the Child control (such as text color and font) in the layoutSubviews method, to set the frame of the subcontrol, you must provide a model attribute to override the set Method of the model. In this method, you can set the model data to the subcontrol. 2. use registerClass In the controller... method to register the XMGDealCell class and use the reuse identifier to find the cell (if there is no registration class, you need to manually create the cell). Passing model data to the cell can also encapsulate the code for creating and obtaining the cell (such as cellWithTableView: method)

    Code custom cell (using autolayout)

    1. Create a subclass that inherits from UITableViewCell. For example, XMGDealCell adds constraints for sub-controls to initWithStyle: reuseIdentifier: method (we recommend that you useMasonryTo set the initialization properties (such as text color and font) of the subcontrol, you must provide a model attribute to override the set Method of the model. In this method, set the model data to the subcontrol. use registerClass In the controller... method to register the XMGDealCell class and use the reuse identifier to find the cell (if there is no registration class, you need to manually create the cell). Passing model data to the cell can also encapsulate the code for creating and obtaining the cell (such as cellWithTableView: method)

    Non-high cell

    Xib custom cell

    Storyboard custom cell

    Code custom cell (frame) code custom cell (Autolayout)

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.