ios對UITableView進行封裝

起因

從事ios工做有段時間了,其中UItableView頻繁被使用中,這個過程當中不斷重複的建立加入代理,好麻煩,並且也讓viewcontroller代碼顯的臃腫,所以作了下面的封裝ios

思路

1.減小重複工做

tableview建立的工做作一次git

2.相似的工做做一次

獲取數據過程當中就把最後須要多少個section,多少個cell的工做作完,後續直接用github

3.是誰的東西就歸誰作

tableviewcell的高度計算,數據填充都讓cell本身去作json

代碼簡單使用樣例

簡單的一個使用樣例
代理

1.viewcontroller

//數據源填充code

-(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相似功能事件

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

//tableviewcell的點擊事件get

-(void)xctableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath model:(id)model{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

2.tableviewcell

設置高度it

-(CGFloat)xctableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

賦值io

-(void)xcSetModel:(id)model forIndexPath:(NSIndexPath *)indexPath{
    _lbName.text = model;
}

現有提供的代理

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;

擴展的功能

1.提供self.dataSource.isXib來設置支持使用xib和純代碼編寫的兩種cell

2.tableviewcell提供parameters能夠傳入請求數據,適用複雜業務須要外層json判斷的

準備加入的(等我有空了就加)

1.tableviewcell中加入curController,獲取當前控制器(代碼被我註釋了,已經有了)

2.重寫tableview和datasource方法

3.對header和footer的封裝

代碼地址:https://github.com/xc-yun/xctableview.

相關文章
相關標籤/搜索