美文网首页iOS控件效果iOS技术难点iOS开发专题
UITableView实现QQ好友列表实战(动态插入删除Cell

UITableView实现QQ好友列表实战(动态插入删除Cell

作者: xFerris | 来源:发表于2016-04-11 11:49 被阅读1591次

    文章已发布在我的博客上,如需转载,请注明原文出处

    实现选择

    网上大部分的教程,都是基于修改section的hearderView来实现的,但是看QQ的好友列表,style是grouped,显然不是使用section的header来处理。使用section的hearderView来实现的,十分简单,网上也有很多源码和教程,只要刷新一下dataSource然后调用就可以了。不在本次讨论的范围之内。

    - (void)reloadSections:(NSIndexSet *)sections
    

    这次我直接使用grouped的cell来做父cell,点击后展开相应的子cell,还有动画特效。(目测QQ好友列表没有使用动画特效,可能是因为好友列表过于大,内存占用问题或者是用户体验问题。)

    封装测试数据

    使用FMDB(或者CoreData)从objc中国获取主issue作为父级cell,文章作为subCell,具体获取使用python和BeautifulSoup,不在本次的讨论范围之内,需要的可以查看相应的资料或者留言我,也可以在文末的项目源码里获取python代码。

    具体实现分析

    TableView一些相关方法介绍

    delegate里和点击有关的方法如下。

    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    

    当有点击事件发生时,运行顺序为。

    • willSelect
    • willDeselect
    • didDeselect
    • didSelect

    插入删除cell的方法为

    - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    

    记得把他们放在

    [table beginUpdates];
    //input insert/delete code here
    [table endUpdates];
    
    逻辑分析

    在didSelect的时候执行插入代码。

            [tableView beginUpdates];
            [tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop];
            [tableView endUpdates];
    
            [tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationRight];
            [tableView endUpdates];
    

    其中的indexArray需要动态计算。类似这样。

            NSMutableArray* indexArray = [NSMutableArray array];
            for (int i=1; i<=masterModel.subIuuses.count; i++) {
                NSIndexPath* path = [NSIndexPath indexPathForRow:i+indexPath.row inSection:indexPath.section];
               [indexArray addObject:path];
            }
    

    可以实现这样的效果。


    问题分析

    看起来没有什么问题。
    但是当点击的是展开的cell下方的cell时,indexPath就会出现问题。像下面这样。


    我要点击的是2x,但是实际上点击的却是4x,问题出在哪里?看这个图片就会发现问题,原来还是那几个方法的执行顺序问题。
    在执行的时候,先执行didDeselect里面的代码,导致插入的cell被删除,indexPath变化,然后再didSelect,当然选中的不是我们想要选中的那个cell了。
    解决方案

    如下图。



    只要willSelect的时候return一个新的indexPath即可,这个indexPath通过计算得出。下面是我的willSelect里的实现代码。

    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell && [cell isMemberOfClass:[SIMMasterTableViewCell class]]) {
            //点击masterCell
            if (_isShowIssues) {
                //展开状态
                if(indexPath.row>_lastSelectMatserIndexPath.row)
                    return [NSIndexPath indexPathForRow:indexPath.row-_lastModelIssuesCount inSection:indexPath.section];
                else
                    return indexPath;
            }
            return  indexPath;
        }
        else if(cell)
        {
            _isSelectSubCell = YES;
            return indexPath;
            //点击到uitableviewcell
        }
        return  indexPath;
    }
    

    最后放上一张效果图。



    项目源码:https://github.com/XFerris/SIMObjc

    相关文章

      网友评论

      • 谈Xx:刚才看了下 总览里没数据,不知道是不是这样
        xFerris:@谈Xx AppStore都审核通过了 当然正常了 你AppStore搜索 objc.io
        谈Xx:@xFerris 网路没问题,你现在也是正常的?
        xFerris:@谈Xx 接口地址:http://objc.xferris.me/objc 初次打开需要从接口同步数据,是不是没有联网?
      • 微笑苍穹iOS:JBWebViewController *webController = [[JBWebViewController alloc] initWithUrl:[NSURL URLWithString:subModel.url] mode:JBWebViewTitleModeNative]; 这里好像没有initWithUrl:mode:的方法吧..Xcode报错l..
        微笑苍穹iOS:@xFerris 谢谢~
        xFerris:谢谢,没有注意到这点,已经写到README.md里了。
        xFerris:@微笑苍穹iOS 对 JBWebViewController 那个是我新增的方法,是为了让WebController更像原生的风格,已经提交给原作者了,他还没有merge我的请求。 到这里https://github.com/XFerris/JBWebViewController,替换pod文件夹里JBWebcontroller的实现文件和头文件就可以。
      • 解忧杂货店老板:写的非常详细,受用了。

      本文标题:UITableView实现QQ好友列表实战(动态插入删除Cell

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