美文网首页
IOS UITableView 高仿微信右侧滑动定位内容

IOS UITableView 高仿微信右侧滑动定位内容

作者: 暖心幽瞳 | 来源:发表于2019-07-25 18:40 被阅读0次

    前言:最近接了一个新项目、项目中有一个需求就是一个UITableView右侧滑动索引定位TableView分区内容、好了、相信大家对这个功能点都有所了解以及见过、那么做成微信的形式咱们是不是得自定义呀、好了、上代码。

    #pragma mark - UITableView Delegate
    - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
        static NSString *identfier = @"XCLeagueFilterCell";
        XCLeagueFilterCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier];
        if (cell == nil) {
            cell = [[XCLeagueFilterCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identfier];
        }
        NSString *key = self.groupNameArr[indexPath.section];
        NSArray *valueArr = [self.dataDic objectForKey:key];
        cell.titlelabel.text = [NSString stringWithFormat:@"%@", valueArr[indexPath.row]];
        cell.deatilLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
        // 第一行显示
        if (indexPath.row == 0) {
            cell.topLineLabel.hidden = NO;
        } else {
            cell.topLineLabel.hidden = YES;
        }
        [cell.logonIv setImage:XCImage(@"score_icon_liansai")];
        return cell;
    }
    
    #pragma mark 索引列点击事件
    // 手指滑动及点击字母相应的代理
    -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
        //点击索引,列表跳转到对应索引的行
        if ([title isEqualToString:@"热门"]) {
            return self.groupNameArr.count;
        }
        [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        //[XCProgressHUD showSuccess:title];
        return index;
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        return [self.groupNameArr objectAtIndex:section];
    }
    
    // 索引标题就是右侧的A-Z 字母
    - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        NSMutableArray *listTitlesArray = [[NSMutableArray alloc] initWithCapacity:[self.groupNameArr count]];
        for (NSString *item in self.groupNameArr) {
            [listTitlesArray addObject:item];
        }
        return listTitlesArray;
    }
    
    - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        NSString *key = self.groupNameArr[section];
        NSArray *valueArr = [self.dataDic objectForKey:key];
        return valueArr.count;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return self.groupNameArr.count;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 50;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    图片中已经注释了几个重点的代理方法、如需要其他功能定制则实现系统其他代理方法。下载地址:https://github.com/chenxuhunoc/WeChatDragIndex

    相关文章

      网友评论

          本文标题:IOS UITableView 高仿微信右侧滑动定位内容

          本文链接:https://www.haomeiwen.com/subject/xvrkrctx.html