類似QQ的分組功能

實現(xiàn)代碼很簡單昼丑,就不多贅述了呻逆。

/** 數(shù)據(jù)源 */

@property (nonatomic,strong)NSMutableArray *sourceData;

/** 記錄展開的section */

@property (nonatomic, strong) NSMutableArray *btnArr;


- (void)viewDidLoad {

[super viewDidLoad];

self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

[self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"header"];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return self.sourceData.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if ([self.btnArr containsObject:[NSNumber numberWithInteger:section]])

{

//包含 展開的

return [self.sourceData[section][@"cellData"] count];

}

else

{

return 0;

}

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *reusid = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusid];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusid];

}

cell.textLabel.text = self.sourceData[indexPath.section][@"cellData"][indexPath.row];

return cell;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];

while (!header.contentView.subviews.count) {

//沒有 需要添加一個button

UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];

[header.contentView addSubview:btn];

btn.backgroundColor = [UIColor whiteColor];

[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

}

UIButton *btn = [header.contentView.subviews firstObject];

[btn setTitle:self.sourceData[section][@"groupTitle"] forState:UIControlStateNormal];

[btn addTarget:self action:@selector(btnClcik:) forControlEvents:UIControlEventTouchUpInside];

btn.tag = section;

return header;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

return 50;

}

-? (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

return 1;

}

- (void)btnClcik:(UIButton *)btn

{

if ([self.btnArr containsObject:[NSNumber numberWithInteger:btn.tag]])

{

//已經(jīng)展開的

[self.btnArr removeObject:[NSNumber numberWithInteger:btn.tag]];

}else

{

[self.btnArr addObject:[NSNumber numberWithInteger:btn.tag]];

}

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationAutomatic];

}

/**

*? 數(shù)據(jù)源的構造 字典數(shù)組

*

*? @return <#return value description#>

*/

- (NSMutableArray *)sourceData

{

if (!_sourceData) {

_sourceData = [[NSMutableArray alloc]init];

for (NSInteger i? = 0 ; i < 5 ; i ++ ) {

NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];

NSArray *arr = [[NSArray alloc]initWithObjects:@"武松",@"西門慶",@"潘金蓮",@"陳翔", nil];

dic[@"cellData"] = arr;

NSString *title = [NSString stringWithFormat:@"第%zd組",i];

dic[@"groupTitle"] = title;

[_sourceData addObject:dic];

}

}

return _sourceData;

}

- (NSMutableArray *)btnArr

{

if (!_btnArr) {

_btnArr = [[NSMutableArray alloc]init];

}

return _btnArr;

}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市菩帝,隨后出現(xiàn)的幾起案子咖城,更是在濱河造成了極大的恐慌,老刑警劉巖呼奢,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宜雀,死亡現(xiàn)場離奇詭異,居然都是意外死亡握础,警方通過查閱死者的電腦和手機辐董,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來禀综,“玉大人简烘,你說我怎么就攤上這事《希” “怎么了孤澎?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長依鸥。 經(jīng)常有香客問我亥至,道長,這世上最難降的妖魔是什么贱迟? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任姐扮,我火速辦了婚禮,結果婚禮上衣吠,老公的妹妹穿的比我還像新娘茶敏。我一直安慰自己,他們只是感情好缚俏,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布惊搏。 她就那樣靜靜地躺著贮乳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪恬惯。 梳的紋絲不亂的頭發(fā)上向拆,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音酪耳,去河邊找鬼浓恳。 笑死,一個胖子當著我的面吹牛碗暗,可吹牛的內(nèi)容都是我干的颈将。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼言疗,長吁一口氣:“原來是場噩夢啊……” “哼晴圾!你這毒婦竟也來了?” 一聲冷哼從身側響起噪奄,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤死姚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后梗醇,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體知允,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年叙谨,在試婚紗的時候發(fā)現(xiàn)自己被綠了温鸽。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡手负,死狀恐怖涤垫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情竟终,我是刑警寧澤蝠猬,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站统捶,受9級特大地震影響榆芦,放射性物質發(fā)生泄漏。R本人自食惡果不足惜喘鸟,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一匆绣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧什黑,春花似錦崎淳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽森爽。三九已至,卻和暖如春嚣镜,著一層夾襖步出監(jiān)牢的瞬間爬迟,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工祈惶, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留雕旨,地道東北人扮匠。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓捧请,卻偏偏與公主長得像,于是被迫代替她去往敵國和親棒搜。 傳聞我的和親對象是個殘疾皇子疹蛉,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容