Ios encapsulates UITableView for details, iosuitableview

Source: Internet
Author: User

Ios encapsulates UITableView for details, iosuitableview
Reason

I have been working on ios for a while, and UItableView is frequently used. In this process, I have repeatedly created and added it to the proxy, Which is troublesome and also makes the viewcontroller code bloated, therefore, the following encapsulation is implemented:

Ideas

1. reduce repetitive work

Tableview is created once.

2. perform similar work once

In the process of getting data, we will finish the work of the last number of sections and number of cells, and then directly use

3. Whoever makes things will do it

Tableviewcell height calculation and data filling are performed by the cell itself

Sample Code

A simple example

1. viewcontroller

// Data source Filling

-(Void) createDataSource {

XCTableViewDataSection * section = [[XCTableViewDataSection alloc] init];

[Section. rowModelsArr addObject: @ "1"];

[Section. rowModelsArr addObject: @ "2"];

[Section. rowModelsArr addObject: @ "3"];

[Section. rowModelsArr addObject: @ "4"];

[Self. dataSource. sections addObject: section];

}

// Similar cellforrow Functions

-(Class) xctableView :( UITableView *) tableView cellClassAtModel :( id) model {

Return [TableViewCell1 class];

}

// Click Event of tableviewcell

-(Void) xctableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath model :( id) model {

[Self. tableView deselectRowAtIndexPath: indexPath animated: YES];

}

2. tableviewcell

Set height

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

Return 100;

}

Assignment

-(Void) xcSetModel :( id) model forIndexPath :( NSIndexPath *) indexPath {

_ LbName. text = model;

}

Existing proxies

1. viewcontroller

/************ Set tableviewcell *******/

// Use the model to determine whether to add different tableviewcells

-(Class) xctableView :( UITableView *) tableView cellClassAtModel :( id) model;

// Use indexpath to determine whether different tableviewcells are added

-(Class) xctableView :( UITableView *) tableView cellClassAtIndexPath :( NSIndexPath *) indexPath;

/******** Proxy for Operation feedback in tableviewcell *******/

-(Void) xctableviewCell :( XCTableViewCell *) cell;

-(Void) xctableviewCell :( XCTableViewCell *) cell model :( id) model;

-(Void) xctableviewCell :( XCTableViewCell *) cell button :( UIButton *) button model :( id) model;

2. tableviewcell

/***** You can use indexpath or data to determine whether to set the height *******/

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

-(CGFloat) xctableView :( UITableView *) tableView heightForRowAtModel :( id) Model;

Extended Functions

1. self. dataSource. isXib is provided to set two types of cells that support xib and pure code writing.

2. tableviewcell provides parameters to pass in the request data. It is applicable to scenarios where complex businesses need to be judged by the outer json

Ready to join (add when I have time)

1. Add curController to tableviewcell to get the current controller (the code has been commented out by me and already exists)

2. Override the tableview and datasource methods.

3. encapsulation of header and footer

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.