本篇主要講解仿QQ分組效果的實現(xiàn),通過本遍的學習,估計都可以自己去實現(xiàn)了(老司機可以),在這里只說仿QQ分組的效果,代碼簡單,邏輯清晰。其他的功能的可以自行添加,好了,進入主題吧。

#PRagma mark - 創(chuàng)建表格- (void)creatTableView { self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, kScreenSize.width, kScreenSize.height-20) style:UITableViewStylePlain]; //設(shè)置代理和數(shù)據(jù)源 self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView];}#pragma mark - 數(shù)據(jù)- (void)dataInit { _dataArr = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < 26; i++) { //一維數(shù)組 NSMutableArray *arr = [[NSMutableArray alloc] init]; for (NSInteger j = 0; j < 10 ; j++) { //每個內(nèi)容都有model填充 WSModel *model = [[WSModel alloc] init]; model.name = [NSString stringWithFormat:@"%C%@",(unichar)('A'+i),@"吳松"]; model.QQNumber = @"qq: 3145419760"; model.sex = @"男"; //把模型對象放入數(shù)組中 [arr addObject:model]; } //把一維數(shù)組放入數(shù)據(jù)源 [_dataArr addObject:arr]; }}這里是用C語言的話說,是二維數(shù)組。這樣的好處是減少代碼量,如果你的需求的中對數(shù)據(jù)有別的要求,您可以定義兩個數(shù)組:sectionArr(分區(qū)),cellArr(分區(qū)cell的個數(shù))2.還有一個比較重要的環(huán)節(jié)
就是為了要對每個分區(qū)的是否展開進行一定的標記,所以筆者就這樣做了:@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ //數(shù)據(jù)源數(shù)組 NSMutableArray *_dataArr; //記錄每個分區(qū) 的展開狀態(tài) 0表示關(guān)閉 1表示展開狀態(tài) int _sectionStatus[26];//默認:關(guān)閉}_sectionStatus[26],因為是26個分區(qū),所以就數(shù)組里的元素的就是26個,默認的話,都是0,所以初始狀態(tài)都是關(guān)閉狀態(tài)3.分區(qū)索引的實現(xiàn)
代碼如下:因為是26個分區(qū),所以for循環(huán)的次數(shù)是26- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { if (self.tableView != tableView) { return nil; } NSMutableArray * titleArr = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < 26; i++) { //設(shè)置26個分區(qū)的索引 [titleArr addObject:[NSString stringWithFormat:@"%C",(unichar)('A'+i)]]; } [titleArr addObject:@"#"]; //返回一個數(shù)組 return titleArr;}實現(xiàn)用戶點擊時,讓其tableView的分區(qū)滾動到指定的分區(qū)位置- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { //選中右側(cè)索引之后 返回 指定分區(qū)的索引值 return index;}4.分區(qū)頭視圖添加點擊事件(重點,關(guān)鍵部分)
顯示分區(qū)內(nèi)容,代碼如下:- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (self.tableView != tableView) { return nil; } UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 40)]; view.backgroundColor = [UIColor lightGrayColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, view.bounds.size.width, 30); button.tag = 101+section; button.backgroundColor = [UIColor yellowColor]; if (_sectionStatus[section] != 0) { [button setTitle:[NSString stringWithFormat:@"%C_%@",(unichar)('A'+section),@"展開中"] forState:UIControlStateNormal]; }else{ [button setTitle:[NSString stringWithFormat:@"%C_%@",(unichar)('A'+section),@"關(guān)閉中"] forState:UIControlStateNormal]; } [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:button]; return view;}這里沒有做相應分裝,只是提供思路給大家下面是點擊分區(qū)頭視圖的相應和實現(xiàn)- (void)btnClick:(UIButton *)button { NSInteger section = button.tag - 101; //跟原來狀態(tài) 取反 _sectionStatus[section] = !_sectionStatus[section]; //只刷新指定分區(qū) [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];}_sectionStatus[section] = !_sectionStatus[section];上面是取反的,同時下面的是刷新指定的分區(qū),記得不要去刷新整個表格,這樣做的話,會很耗費其性能大家看到這里估計都明白了吧,是不是很簡單呢?小結(jié)
最后筆者要說一下,在做次功能的時候,遇到了一個坑,因為筆者的公司要求,項目里所有的本地圖片都是SVG的,很耗性能,所以在筆者點擊展開分區(qū)時,就會項目閃退,因為就是因為頻繁加載SVG圖片。最后筆者把加載SVG的代碼放到了awakeFromNib方法中,當然了,有時候也是解決不了耗性能問題,建議還是使用PNG。源代碼
本篇文章對應的源代碼下載地址:https://github.com/WSmalan/-QQ-
新聞熱點
疑難解答
圖片精選