简体   繁体   English

单击按钮展开 tableview 单元格

[英]Expand tableview cell on button click

I did some easy trick with constraints.我用约束做了一些简单的技巧。 I made tableView cell which is expandable.我制作了可扩展的tableView cell On cell click it opens new cell( black )(Below is image for easier understanding).cell单击它会打开新单元格(黑色)(下图是为了更容易理解)。

My question is how can I make it work with the button which is inside the top cell which looks like "V" .我的问题是如何使它与顶部单元格内看起来像"V"的按钮一起工作。 I want it at least to rotate like 90° if cell is clicked.如果单击cell我希望它至少旋转 90°。 How could I achieve it.我怎么能做到。

This is how I made it to expand:这就是我让它扩展的方式:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if(selectedIndex == indexPath.row){
            return 210
        }else{
            return 135
        }
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if (selectedIndex == indexPath.row){
            selectedIndex = -1
        }else{
            selectedIndex = indexPath.row
        }

        self.productstable.beginUpdates()
        self.productstable.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        self.productstable.endUpdates()
    }

You can achieve using closure:您可以使用闭包实现:

typealias arrowButtonTappedBlock = (button:UIButton) -> Void

declare a closure in your custom cell like below:在您的自定义单元格中声明一个closure ,如下所示:

var arrowButtonTapped : arrowButtonTappedBlock!

and on arrow button click's action call the closure like below:并在箭头按钮上单击操作调用闭包,如下所示:

if arrowButtonTapped != nil {
            arrowButtonTapped(button: sender as! UIButton)
        }

and in cellForRowAtIndexPath : set the closure and perform the action you were performing in didSelectRowAtIndexPath:cellForRowAtIndexPath :设置闭包并执行您在didSelectRowAtIndexPath:执行的操作didSelectRowAtIndexPath:

cell!.arrowButtonTapped = { (button:UIButton) -> Void in

                if (self.selectedIndex == indexPath.row){
                    self.selectedIndex = -1
                }else{
                    self.selectedIndex  = indexPath.row
                }

                self.tableview.beginUpdates()
                self.tableview.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
                self.tableview.endUpdates()
} 

You can do this with easy way你可以用简单的方法做到这一点

  1. Delare var selectedRowIndex = -1 Delare var selectedRowIndex = -1

  2. Create Protocol for your Custom Cell为您的自定义单元创建协议

    protocol TableViewCellDelegate { func BtnExpendTapped(_ cell: CustomCell) }
  3. In your Custom Cell Declare protocol and add action method for button在您的自定义单元声明协议中并为按钮添加操作方法

    var delegate: TableViewCellDelegate?

     @IBAction func actionExpendBtnTap(_ sender: UIButton){ delegate?.BtnExpendTapped(self) }
  4. In your Height for Row method add your Expended and normal height as your requirement在您的 Height for Row 方法中,根据您的要求添加您的 Expended 和 normal 高度

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.row == selectedRowIndex { return 270 //Expanded } return 138.5 //Not expanded }
  5. use your protocol delegate in view controller and add delegate method在视图控制器中使用您的协议委托并添加委托方法

func BtnExpendTapped(_ cell: CustomCell) {
     if let indexPath = tblView.indexPath(for: cell) {
         if selectedRowIndex == indexPath.row {
             selectedRowIndex = -1
         } else {
             selectedRowIndex = indexPath.row
         }
        tblView.reloadRows(at: [indexPath], with: .none)
     }
 }


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM