UITableView分組索引

使用系統(tǒng)的方法設(shè)置娶桦,效果圖如下:

效果圖.png

部分初始化

    // 數(shù)據(jù)源
    _dataModelArray = [[NSMutableArray alloc] init];
    // 設(shè)置索引
    _charArray = [[NSMutableArray alloc] init];
    // 索引字體顏色
    _tableView.sectionIndexColor = [UIColor colorWithHexString:@"686a6c"];
    // 索引背景色
    _tableView.sectionIndexBackgroundColor = [UIColor clearColor];

數(shù)據(jù)源的添加

                for (char key = 'A'; key<='Z'; key++) {
            
                    NSArray * eachArray = [memberArrayDic objectForKey:[NSString stringWithFormat:@"%c",key]];
                    if (eachArray) {
                        // 添加索引
                        [_charArray addObject:[NSString stringWithFormat:@"%c",key]];
                        
                        NSMutableArray * eachModelArray = [[NSMutableArray alloc] init];
                        for (NSDictionary * dict in eachArray) {
                            GroupUserModel * model = [GroupUserModel yy_modelWithDictionary:dict];
                            [eachModelArray addObject:model];
                        }
                        [_dataModelArray addObject:eachModelArray];
                    }

                }
                
                NSArray * otherArray = memberArrayDic[@"#"];
                if (otherArray) {
                    [_charArray addObject:@"#"];
                    
                    NSMutableArray * otherModelArray = [[NSMutableArray alloc] init];
                    for (NSDictionary * dic in otherArray) {
                        GroupUserModel * model = [GroupUserModel yy_modelWithDictionary:dic];
                        [otherModelArray addObject:model];
                    }
                    
                    [_dataModelArray addObject:otherModelArray];
                }

UITableView設(shè)置

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_dataModelArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 22;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dataModelArray[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * identifier = @"cell";
    GroupSettingUserCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"GroupSettingUserCell" owner:self options:nil].firstObject;
    }
    
    GroupUserModel * model = _dataModelArray[indexPath.section][indexPath.row];
    cell.userName = model.chatName;
    if ([model.selectedType isEqualToString:@"1"]) {
        cell.isSelected = YES;
    } else {
        cell.isSelected = NO;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    GroupUserModel * model = _dataModelArray[indexPath.section][indexPath.row];
    if ([model.selectedType isEqualToString:@"0"]) {
        model.selectedType = @"1";
        _selectedUserCount ++;
    } else {
        model.selectedType = @"0";
        if (_selectedUserCount) {
            _selectedUserCount --;
        }
    }
    _upView.selectedCount = _selectedUserCount;
    [_tableView reloadData];
}
// 索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return _charArray;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    NSLog(@"===%@  ===%ld",title,(long)index);
    
    //點(diǎn)擊索引残吩,列表跳轉(zhuǎn)到對應(yīng)索引的行
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]
                     atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
    return index;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 22)];
    headView.backgroundColor = [UIColor colorWithHexString:@"ebebeb"];

    //標(biāo)題文字
    UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 40, 22)];
    title.backgroundColor = [UIColor clearColor];
    title.font = [UIFont systemFontOfSize:12];
    title.textColor = [UIColor colorWithHexString:@"a2a4a7"];
    title.text = _charArray[section];
    [headView addSubview:title];
    
    return headView;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市弱睦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌脓豪,老刑警劉巖滨砍,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異纷跛,居然都是意外死亡喻括,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進(jìn)店門贫奠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來唬血,“玉大人,你說我怎么就攤上這事唤崭】胶蓿” “怎么了?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵谢肾,是天一觀的道長腕侄。 經(jīng)常有香客問我,道長芦疏,這世上最難降的妖魔是什么冕杠? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮眯分,結(jié)果婚禮上拌汇,老公的妹妹穿的比我還像新娘。我一直安慰自己弊决,他們只是感情好噪舀,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布魁淳。 她就那樣靜靜地躺著,像睡著了一般与倡。 火紅的嫁衣襯著肌膚如雪界逛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天纺座,我揣著相機(jī)與錄音息拜,去河邊找鬼。 笑死净响,一個胖子當(dāng)著我的面吹牛少欺,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播馋贤,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼赞别,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了配乓?” 一聲冷哼從身側(cè)響起仿滔,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎犹芹,沒想到半個月后崎页,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡腰埂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年飒焦,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盐固。...
    茶點(diǎn)故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡荒给,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出刁卜,到底是詐尸還是另有隱情,我是刑警寧澤曙咽,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布蛔趴,位于F島的核電站,受9級特大地震影響例朱,放射性物質(zhì)發(fā)生泄漏孝情。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一洒嗤、第九天 我趴在偏房一處隱蔽的房頂上張望箫荡。 院中可真熱鬧,春花似錦渔隶、人聲如沸羔挡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绞灼。三九已至利术,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間低矮,已是汗流浹背印叁。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留军掂,地道東北人轮蜕。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像蝗锥,于是被迫代替她去往敵國和親肠虽。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評論 2 354

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

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理玛追,服務(wù)發(fā)現(xiàn)税课,斷路器,智...
    卡卡羅2017閱讀 134,656評論 18 139
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件痊剖,我們平時使用的軟件中到處都可以看到它的影子韩玩,類似...
    liudhkk閱讀 9,043評論 3 38
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件陆馁、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,103評論 4 62
  • 今天看電視上一個紀(jì)錄片叮贩,采訪到某個國企老工人击狮,片子介紹道:這名老工人在某某崗位上12年,把寶貴的青春都奉獻(xiàn)給XX事...
    小火咕嘟閱讀 542評論 0 0
  • 越是難受時益老,越是需要學(xué)習(xí)時彪蓬,學(xué)習(xí)不一定指看教科書,而是尋找解決問題的方法捺萌,更忌自己胡思亂想档冬,沉溺情緒。 要慢慢的讓...