UITableView的折疊收縮和QQ好友分組效果

可折疊展開的tableView,QQ好友分組列表

demo下載地址https://github.com/zhengwenming/ExpandTableView


原理分析:這個(gè)可以折疊的table,我們點(diǎn)擊的是table的section,每個(gè)section下面都對(duì)應(yīng)一個(gè)數(shù)組,點(diǎn)擊section,就展開sction然后展示數(shù)組數(shù)據(jù)。每次點(diǎn)擊section都要刷新當(dāng)前點(diǎn)擊的這個(gè)section,不用reloadData,提高效率。那么點(diǎn)擊的這個(gè)sction怎么知道自己是展開呢還是折疊起來呢?那么關(guān)鍵就是對(duì)這里的處理,我們自己要加一個(gè)條件判斷是展開還是折疊。下面上代碼看看具體實(shí)現(xiàn)。

方法一

方法一的原理是用一個(gè)stateArray去記錄每個(gè)section的狀態(tài),當(dāng)然光記錄還是不行的,還是不斷的改變這個(gè)stateArray對(duì)應(yīng)section的值,展開了就把值替換為1,閉合了替換了0.那么這個(gè)0和1就是我們的依據(jù),依據(jù)這個(gè)就可以返回這個(gè)scetion對(duì)應(yīng)的row了。

給section中的按鈕添加點(diǎn)擊事件,然后設(shè)置按鈕的tag值為section。

方法二

方法二中用一個(gè)類去記錄和不斷的替換狀態(tài),用一個(gè)model類。看看這個(gè)model類的定義

再看看怎么用

(UIView?)tableView:(UITableView?)tableView viewForHeaderInSection:(NSInteger)section?

{?

UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];?

sectionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8];?

GroupModel *groupModel = dataSource[section];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];?

[button setFrame:sectionView.bounds];?

[button setTag:section];?

[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];?

[button setTitle:groupModel.groupName forState:UIControlStateNormal];?

[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];?

[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];?

[sectionView addSubview:button];?

UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)];?

[line setImage:[UIImage imageNamed:@”line_real”]];?

[sectionView addSubview:line];

UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (44-16)/2, 14, 16)];?

[imgView setImage:[UIImage imageNamed:@”ico_list”]];?

[sectionView addSubview:imgView];

UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, (44-20)/2, 40, 20)];?

[numberLabel setBackgroundColor:[UIColor clearColor]];?

[numberLabel setFont:[UIFont systemFontOfSize:14]];?

NSInteger onLineCount = 0;?

for (NSDictionary *friendInfoDic in groupModel.groupFriends) {?

if ([friendInfoDic[@”status”] isEqualToString:@”1”]) {?

onLineCount++;?

}?

}?

[numberLabel setText:[NSString stringWithFormat:@”%ld/%ld”,onLineCount,groupModel.groupCount]];?

[sectionView addSubview:numberLabel];

return sectionView;?

}?

- (void)tableView:(UITableView?)tableView didSelectRowAtIndexPath:(NSIndexPath?)indexPath?

{?

GroupModel *groupModel = dataSource[indexPath.section];?

NSDictionary *friendInfoDic = groupModel.groupFriends[indexPath.row];?

NSLog(@”%@ %@”,friendInfoDic[@”name”],friendInfoDic[@”shuoshuo”]);?

}

(void)buttonPress:(UIButton *)sender//headButton點(diǎn)擊?

{?

GroupModel *groupModel = dataSource[sender.tag];?

groupModel.isOpened = !groupModel.isOpened;?

[expandTable reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic];?

}

看看關(guān)鍵代碼,就是按鈕點(diǎn)擊事件里面buttonPress,這一句話?

groupModel.isOpened = !groupModel.isOpened;?

的意思是如果是展開,那么點(diǎn)擊之后就是折疊,?

如果是折疊,點(diǎn)擊之后就是展開。這個(gè)狀態(tài)被記錄下來了,而且可以不斷的改變。

好,到此為止。兩種方法介紹完畢。想看demo的同學(xué)可以直接到Github上下載https://github.com/zhengwenming/ExpandTableView

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 最近忙完項(xiàng)目比較閑,想寫一篇博客來分享一些自學(xué)iOS的心得體會(huì),希望對(duì)迷茫的你有所幫助。博主非科班出身,一些...
    GitHubPorter閱讀 1,603評(píng)論 9 5
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,316評(píng)論 3 38
  • 插入行。刪除行。折疊區(qū)。設(shè)置索引。刷新表格。注冊(cè)cell。 //UITableView的ADD // NSStri...
    nothing_c閱讀 308評(píng)論 0 0
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,752評(píng)論 1 14
  • iOS開發(fā)系列--網(wǎng)絡(luò)開發(fā) 概覽 大部分應(yīng)用程序都或多或少會(huì)牽扯到網(wǎng)絡(luò)開發(fā),例如說新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 4,066評(píng)論 2 7

友情鏈接更多精彩內(nèi)容