標籤:ext 工作 atm 頻繁 cti for 一個 markdown 請求
---恢複內容開始---
原由
從事ios工作有段時間了,其中UItableView頻繁被使用中,這個過程中不斷重複的建立加入代理,好麻煩,而且也讓viewcontroller代碼顯的臃腫,因此做了下面的封裝
思路1.減少重複工作
tableview建立的工作做一次
2.類似的工作作一次
擷取資料過程中就把最後需要多少個section,多少個cell的工作做完,後續直接用
3.是誰的東西就歸誰做
tableviewcell的高度計算,資料填充都讓cell自己去做
代碼簡單使用範例
簡單的一個使用範例
1.viewcontroller
//資料來源填充
-(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的點擊事件
-(void)xctableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath model:(id)model{ [self.tableView deselectRowAtIndexPath:indexPath animated:YES];}
2.tableviewcell
設定高度
-(CGFloat)xctableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 100;}
賦值
-(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和純程式碼編寫的兩種cell2.tableviewcell提供parameters可以傳入請求資料,適用複雜業務需要外層json判斷的準備加入的(等我有空了就加)1.tableviewcell中加入curController,擷取當前控制器(代碼被我注釋了,已經有了)2.重寫tableview和datasource方法3.對header和footer的封裝
代碼地址:https://github.com/xc-yun/xctableview.
---恢複內容結束---
ios對UITableView進行封裝