UITableView索引、cell动画

作者: smalldu | 来源:发表于2016-03-04 22:55 被阅读1181次

    UITableView可以说是iOS开发中最常用的组件,今天来谈谈UITableView如何实现索引和cell添加动画。---今天的话题比较轻松,不用看头疼的Core... ...

    看看最终要实现的效果先:

    配图

    动画效果是随便写的最简单的平移动画,大家可以根据自己的喜好来写

    首先新建一个Single View Controller的项目,然后在main.stroyboard 价格tableView ,设置好代理,在viewController中加一个tableView的变量。

    首先来看看索引,所以其实就需要一个数组,这里我先写了一个数据源数组

    //随便写几个城市
        private let citys = ["西安","北京","上海","广州","深圳","杭州","重庆","南京","济南","湖北","洛阳","西宁","半岛","泸州","天津","桂林","大兴安岭","骡马市","三峡","白水","常州","盐城","怀化","湘潭","定西","天水"]
    

    然后我们需要按照文字的首字母进行排序,这里写一个String的扩展,根据汉子获得对应的拼音。

    extension String {
        
        func toPinYin() -> String {
            let mutableString = NSMutableString(string: self)
            //把汉字转为拼音
            CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
            //去掉拼音的音标
            CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false)
            let string = String(mutableString)
            //去掉空格
            return string.stringByReplacingOccurrencesOfString(" ", withString: "")
        }
    }
    

    然后再声明两个变量

      var titles = [String]()  //放section的标题
      var cityDics = [String:[String]]()  //放每个section的元素数组
    

    很明确就是为了分组,下面写一个工具方法处理数据源,给这两个变量赋值

    /**
         生成字典
         */
        func generalCityDics(){
            for city in citys{
                //将城市转成拼音  然后取第一个字母 然后大写
                let key = city.toPinYin().substringToIndex(city.toPinYin().startIndex.advancedBy(1)).uppercaseString
                //判断cityDics是否已经存在此key
                if var cityValues = cityDics[key]{
                    //拿出values 把city添加进去
                    cityValues.append(city)
                    //重新赋值
                    cityDics[key] = cityValues
                    
                }else{
                    //第一次赋值
                    titles.append(key)
                    cityDics[key] = [city]
                }
                //排序
                titles = titles.sort{ $0<$1 }
            }
        }
    

    循环取拼音的第一个字母大写后当key,给key赋值数组,在viewDidLoad中调用这个方法就行了。

    先实现tableview的三个数据源的方法,这些都是非常常用的操作,建议写成code snippet

    以后用的时候就会比较快的输出,提高coding效率

    配图

    有些同学可能不知道怎么创建这种片段,下面附教程

    配图

    简单的就这样啊,当然还可以创建预设参数,<#param#>

        // MARK: - tableView cell单元格信息
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
            cell.textLabel?.text =  self.cityDics[self.titles[indexPath.section]]![indexPath.row]
            return cell
        }
        
        
        // MARK: - 返回行数
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.cityDics[self.titles[section]]!.count
        }
        
        /**
         section数
         */
        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return self.titles.count
        }
    
        /**
         标题
         */
        func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return self.titles[section]
        }
        
        /**
         索引数组
         */
        func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
            return self.titles
        }
    

    然后就是实现tableview的这几个方法就可以了,运行项目 就有索引了。是不是so easy呀!

    那么怎么实现cell的动画呢,可能比这个还要easy。就一个方法里面写动画就行啦

    只要在 willDisplayCell中加上动画就行了

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
            
            let rotationTransform = CATransform3DTranslate(CATransform3DIdentity,200,
                50, 0)
            cell.layer.transform = rotationTransform
            UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in
                cell.layer.transform = CATransform3DIdentity
                }, completion: nil)
            
        }
    

    我这里写的简单的平移动画,大家可以试试其他效果。

    代码比较简单就不上传了,希望大家喜欢

    相关文章

      网友评论

      本文标题:UITableView索引、cell动画

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