主要感覺最近沒什么好寫的~就簡單整理一下自己的思路吧卦睹,折疊是一個很簡單的效果我寫寫越妈,大家可以看看泪勒,跌前,
折疊的實現(xiàn)有幾個方法效果比較好的就是把折疊之前的看到的做成section的頭視圖棕兼,把折疊開的部分做成section中的row,加上數(shù)據(jù)的配合就能實現(xiàn)折疊效果了抵乓。
按上面說的那數(shù)據(jù)的效果應(yīng)該是數(shù)據(jù)數(shù)組中存放的是字典(字典對應(yīng)每個section其中必有的兩個字段是isOpen:是否下拉被打開程储,childs:下面rows的數(shù)據(jù)數(shù)組),要是數(shù)據(jù)對象也是同理可證了臂寝。
下面是我做的折疊UITableViewDataSource代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//我使用的是Model
if (self.dataArray[section].isOpen) {//判斷是否打開狀態(tài)章鲤,打開狀態(tài)返回childs數(shù)組的個數(shù)
return [self.dataArray[section].childs count];
}
return 0;//沒打開返回0}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.dataArray.count;//返回section的個數(shù)
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
。咆贬。败徊。。掏缎。皱蹦。//返回頭視圖的樣式自定義的但是要確保可以點擊眷蜈,沪哺,有點擊事件,我的頭視圖上面有個按鍵酌儒,其點擊事件為imageButtonClick:
return headView;
}
-(void)imageButtonClick:(UIButton *)button{
if (self.dataArray[button.tag - buttonTagBaseNumber].isOpen) {//buttonTagBaseNumber是一個tag值的基數(shù)確保tag不從0開始,將開關(guān)變成NO辜妓,刷新單個section將列表打開(ps:個人感覺UITableViewRowAnimationNone的刷新效果最好)
self.dataArray[button.tag - buttonTagBaseNumber].isOpen = NO;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:(button.tag - buttonTagBaseNumber)] withRowAnimation:UITableViewRowAnimationNone];
}else{
//如果是正在打開狀態(tài)判斷當(dāng)前section下有沒有數(shù)據(jù)如果有數(shù)據(jù),將開關(guān)變成YES忌怎,刷新單個section將列表打開
if ([self.dataArray[(button.tag - buttonTagBaseNumber)].childs count] != 0) {
self.dataArray[button.tag - buttonTagBaseNumber].isOpen = YES;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:(button.tag - buttonTagBaseNumber)] withRowAnimation:UITableViewRowAnimationNone];
//將打開的section滾動到屏幕中間
NSIndexPath * index = [NSIndexPath indexPathForRow:0 inSection:(button.tag - buttonTagBaseNumber)];
[self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}else{//如果section下沒有數(shù)據(jù)籍滴,直接給出提示,不做任何操作
[self showMessage:@"親這個暫無分類~"];
}
}
}
//選中其中一條的時候?qū)ection關(guān)閉
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPat{
self.dataArray[indexPat.section].isOpen = NO;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:(indexPat.section)] withRowAnimation:UITableViewRowAnimationNone];
}