2016-09-24 100 views
3

我有一個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

回答

2

創建一個數組來保存哪些單元格應該展開。當按下按鈕時,相應地添加或移除一個元素到數組中。這裏是代碼:

// 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

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

相關問題