ios對UITableView進行封裝

來源:互聯網
上載者:User

標籤: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進行封裝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.