2016-12-23 17 views
0

UIImageViewを表示および非表示にする最良の方法はUIButtonをクリックしてUITableViewCell(カスタムクラス)をクリックします。テーブルビューのセルでUIImageViewを表示および非表示にする

実際には私の行の高さはUITableViewAutomaticDimensionで計算されており、必要な制約がすべて設定されています。

問題:セルの高さまたはUIImageViewの高さを変更する方が良いですか?

UIImageViewの高さの制約を変更すると、セルが再ロードされるまで高さは変わりません。

提案がありますか?

+0

言いますか? –

+0

reloadData()が呼び出されない限りNo –

+0

あなたのtableviewcellスクリーンショットを提供してください。私たちはあなたが望むものを理解することができますか? –

答えて

0

UITableViewCellのカスタムクラスはCustomTableCellとしましょう。 CustomTableCellであなたはUIImageViewの出口に出ます(imageViewと言う)。 UIButtonの対策を講じてください(btnShow)。

以下の方法を実装します。

まず、あなたのVCでのグローバルな配列を取る、arrSelected

は私達があなたのImageViewのの高さを変更した場合、セルの高さが影響を受けるかをしなければならない
public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{ 
      return UITableViewAutomaticDimension 
    } 

    open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
      return UITableViewAutomaticDimension 
    } 

open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
     let customCell : CustomTableCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableCell 

     customCell.imageView.tag = indexPath.row; 
     customCell.btnShow.tag = indexPath.row; 

    var xPos : CGFloat! 
    var yPos : CGFloat! 
    var width : CGFloat! 
    var height : CGFloat! 

    xPos = cell.imageView.frame.origin.x 
    width = cell.imageView.frame.size.width 
    height = cell.imageView.frame.size.height 

    if arrSelected.contains(indexPath.row) { 
     yPos = 100 // whatever the height of imageview when image is present 
    cell.imageView.isHidden = false 

    } 
    else{ 
    yPos = cell.btnShow.frame.size.height 
    cell.imageView.isHidden = true 
    } 

     cell.imageView.frame = CGRect(x: xPos, y: yPos, width: width, height: height)   

     return customCell 
    } 

    // btnShow action 

    @IBAction func actionbtnShow(_ sender: UIButton) { 

    if sender.isSelected { 
     let itemToRemoveIndex = arrSelected.indexOf(sender.tag) { 
    arrSelected.removeAtIndex(sender.tag) 
     sender.isSelected = !sender.isSelected 

    } 
    else{ 
     arrSelected.append(sender.Tag) 
     sender.isSelected = !sender.isSelected 
    } 

    // reload tableView 
    } 
-2

アニメーションブロックで非表示タグを設定し、tableViewのbeginUpdatesとendUpdateを同じブロックにパックすることができます。

のObjective-Cでまた
UIView.animate(withDuration: 0.2, animations:{ 
    imageView.isHidden = false 
    tableView.beginUpdates() 
    tableView.endUpdates() 
}) 

:このように

[UIView animateWithDuration:0.2 animations:^{ 
    imageView.hidden = NO; 
    [tableView beginUpdates]; 
    [tableView endUpdates]; 
}]; 

、セルの高さは、アニメーションに成長する必要があります。

+0

これはSwiftではありません –

+0

あなたは私の答えを更新させてください。 – choli

+0

これは彼女の質問の答えではなく、彼女はアニメーションについて尋ねていません。 –

関連する問題