UITableview:单击自定义按钮时动态更改单元格高度

问题描述:

我有一个UITableview,其中每个单元格都有一个按钮。 我的问题是,如果我点击第一行中的按钮,单元格的高度增加,然后我单击tableviewcell中的另一个按钮已扩展单元格高度将减少和选定单元格高度将增加UITableview:单击自定义按钮时动态更改单元格高度

After尝试此链接UITableView: How to change cell height dynamically when a button is clicked in it? Swift

这里是我的代码:

var indexOfExpendedCell:NSInteger = -1 
    var shouldCellBeExpanded:Bool = false 


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     cell.optionButton?.addTarget(self, action: #selector(ViewController.optionAction), forControlEvents: UIControlEvents.TouchUpInside) 
     return cell 
    } 

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

     if shouldCellBeExpanded && indexPath.row == indexOfExpendedCell { 

     return 102 
     } 
     else { return 57 } 
    } 

    } 

    func optionAction(sender: UIButton){ 

    if let button = sender as? UIButton { 
     shouldCellBeExpanded = !shouldCellBeExpanded 
     indexOfExpendedCell = sender.tag 
     if shouldCellBeExpanded { 
      self.tableView.beginUpdates() 
      self.tableView.endUpdates() 

      button.setImage(UIImage(named: "Reject Icon filled"), forState: .Normal) 
     } 

     else { 
      self.tableView.beginUpdates() 
      self.tableView.endUpdates() 

      button.setImage(UIImage(named: "Reject Icon"), forState: .Normal) 
     } 

    } 
    } 

尝试此代码后,细胞高度扩大和缩小,当我在同一行中单击按钮。我没有做到以下步骤

步骤: 1.单击第二行单元格的高度扩展 2.现在点击第一行,我需要减少第二排的高度和扩大第一行

的高度

任何人都可以帮助我吗?

+0

我想你似乎需要有两个数组变量来存储您的单元格的可扩展状态和扩展索引的信息。所以你可以利用这些数组来跟踪单元在你的optionAction函数或tableview委托函数中的状态。 – woogii

+0

@woogii谢谢你的回复。 – Aravi

创建一个数组来保存哪些单元格应该展开。当按下按钮时,相应地添加或移除一个元素到数组中。这里是代码:

// Initialize an empty array of integers 
var expandedCells = [Int]() 

@IBAction func buttonPressed(_ sender: AnyObject) { 
    // If the array contains the button that was pressed, then remove that button from the array 
    if expandedCells.contains(sender.tag) { 
     expandedCells = expandedCells.filter({ $0 != sender.tag}) 
    } 
    // Otherwise, add the button to the array 
    else { 
     expandedCells.append(sender.tag) 
    } 
    // Reload the tableView data anytime a button is pressed 
    tableView.reloadData() 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell 
    // Set the tag on your button to be equal to indexPath.row 
    // This allows @IBAction func buttonPressed(_ sender: AnyObject) above to discern which row was tapped 
    cell.myButton.tag = indexPath.row 
    return cell 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    // Set the row height based on whether or not the Int associated with that row is contained in the expandedCells array 
    if expandedCells.contains(indexPath.row) { 
     return 102 
    } else { 
     return 57 
    } 
} 
+0

对不起,我仍然有同样的问题。我点击第二个单元格,它没有减少已扩展的单元格高度 – Aravi

+0

谢谢,我添加前删除了数组中的所有值。现在它工作正常 – Aravi

+0

太棒了,很高兴它的作品。 –