iOS UITableView encapsulation

Source: Internet
Author: User

---restore content starts---

Originally by

Engaged in iOS work for a while, where UITableView is frequently used, the process is repeatedly created to join the agent, good trouble, but also let the Viewcontroller code significantly bloated, so did the following package

Train of thought 1. Reduce duplication of effort

TableView create a job to do once

2. Similar work is done once

Get the data in the process of the last need to how many sections, how many cell work done, followed by direct use

3. Who will do the thing?

Tableviewcell the height of the calculation, the data is populated to let the cell do its own

Simple Code Usage Example

A simple example of using a sample

1.viewcontroller

Data source Padding

-(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];}

Cellforrow similar functions

-(Class)xctableView:(UITableView *)tableView cellClassAtModel:(id)model{    return [TableViewCell1 class];}

Tableviewcell Click events

-(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;}

Assign value

-(void)xcSetModel:(id)model forIndexPath:(NSIndexPath *)indexPath{    _lbName.text = model;}
Available proxies 1.viewcontroller
/************设置tableviewcell *******///通过model判断加入不同的tableViewCell-(Class)xctableView:(UITableView *)tableView cellClassAtModel:(id)model;//通过indexpath判断加入不同的tableViewCell-(Class)xctableView:(UITableView *)tableView cellClassAtIndexPath:(NSIndexPath *)indexPath;/********tableviewcell中操作反馈作用的代理 *******/-(void)xctableviewCell:(XCTableViewCell *)cell;-(void)xctableviewCell:(XCTableViewCell *)cell model:(id)model;-(void)xctableviewCell:(XCTableViewCell *)cell button:(UIButton *)button model:(id)model;
2. Tableviewcell
/*****提供通过indexpath和数据两种方法判断设置高度 *******/-(CGFloat)xctableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;-(CGFloat)xctableView:(UITableView *)tableView heightForRowAtModel:(id)Model;
Extended functionality 1. Provide SELF.DATASOURCE.ISXIB to set up two Cell2.tableviewcell that support the use of xib and pure code to provide parameters can pass in the request data, for the complex business needs of the outer JSON to determine the readiness to join (when I am free to add) 1 . Tableviewcell add Curcontroller to get the current controller (code has been commented by me, already has) 2. Overriding TableView and DataSource methods 3. Encapsulation of headers and footer

Code address: Https://github.com/xc-yun/xctableview.

---restore content ends---

iOS UITableView encapsulation

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.