2016-09-24 4 views
3

各セルにボタンがあるUITableviewがあります。 私の問題は、最初の行のボタンをクリックすると、セルの高さが上がり、次にtableviewcellの別のボタンをクリックすると、すでにセルの高さが縮小され、選択されたセルの高さが増えます。ここでは、このリンクUITableView: How to change cell height dynamically when a button is clicked in it? SwiftUITableview:カスタムボタンをクリックするとセルの高さを動的に変更

をしようとすると、私のコードです:

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番目の行の高さを低くする必要がある最初の行をクリックして、最初の行の高さを拡張

2を展開されている第二列をクリックしてください

誰でも助けてくれますか?

+0

私は、あなたのセルの拡張可能なステータスと拡張インデックスの情報を格納する2つの配列変数を持つ必要があるようだと思う。したがって、これらの配列を使用して、optionAction関数またはtableview delegate関数のいずれかでセルの状態を追跡することができます。 – 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

申し訳ありませんが、私は同じ問題がまだあります。 2番目のセルをクリックすると、既にセルの高さが縮小されていません – Aravi

+0

ありがとうございました。追加する前に配列のすべての値を削除しました。今それはうまく動作します – Aravi

+0

素晴らしい、うれしいそれは動作します。上記のコードでもう一度 –

関連する問題