自定义Cell伸缩

思路:

1. 点击黄色的Button我们其实想要得到是Cell所在的行数,从而刷表,从新调用表的 heightForRowAtindexPath 代理,将所选中的cell的高进行改变,从而得到我们所看到的伸展cell。

2.难点:

  (1).在cell中自定义的button怎么获取cell的indexPath ?

  (2).在heightForRowAtindexPath 代理 中如何让判断所要改变的行是伸展换是收缩 ?

3. 解决

    (1)在Button的点击事件中获取Cell的indexpath:

     let tagViewcell = sender.superview?.superview  as! UITableViewCell   (注意父类的层次结构)

     let  indexpath = tableView.indexPath(for: tagViewcell)


  (2)强制刷行点击的行:  刷新行

         tableView.reloadRows(at: [IndexPath(row: (indexpath?.row)! , section:0)] , with: .automatic)


  (3)利用数组解决判断要改变的行:

     在点击button的事件中判断  if let index = selectedCellArray.index(of: (indexpath?.row)!) 若存在则返回元素所在数组的下表,根据下标删除数组中的元素 selectedCellArray.remove(at: index), 若不存在在数组中添加selectedCellArray.append((indexpath?.row)! ) // 将行数存储起来

示例:

  if let index = selectedCellArray.index(of: (indexpath?.row)!)  {

            

            selectedCellArray.remove(at: index  )

            

        }else{

            

            selectedCellArray.append((indexpath?.row)! ) // 将行数存储起来

            

        }


    在表的行高代理中判断,indexpath?.row 是否包含在selectedCellArray中。

示例:

 if selectedCellArray.contains(indexPath.row) {  // 对比是比较将行数

            return 200

          }else{

            return 33

        }









相关文章: