2017-02-03 9 views
2

要件;スウィフトtableView.reloadRowsセルの削除

私はスタティックUITableViewを持っていて、セルの1つをクリックするとpickerviewを開こうとしています。 heightForRowAtでサイズを変更しますので、cellをクリックするとサイズが変わるようにreloadする必要があります。私はreloadData()を使用している場合、それは完璧に動作します(私はそう、私はアニメーションを追加することができますreloadRowsを使用したい)

問題:

私はreloadRows(at..., with: .automatic)行が単に消えしようとします。 reloadRowsviewWillAppearに変更されても、行は消えません。

コード:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print(indexPath) 
    if colorCelllExpandedIndex != nil{ 
     let prev = colorCelllExpandedIndex 
     colorCelllExpandedIndex = nil 
     tableView.reloadRows(at: [prev!], with: .automatic) 
     //tableView.reloadData() 
    } else { 
     colorCelllExpandedIndex = indexPath 
     colorCell.frame.size.height = CGFloat(colorCellExpandedHeight) 
     tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
    } 

} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    if indexPath == colorCelllExpandedIndex { 
     return CGFloat(colorCellExpandedHeight) 
    } else { 
     if indexPath.row == 1 { 
      return CGFloat(colorCellRegularHeight) 
     } 
     return UITableViewAutomaticDimension 
    } 
} 

画面イメージ:

before and after clicking the color button

How it looks like if I use reloadData

答えて

2

このようなときreloadRows(at:with:)などの機能を持ついくつかの問題があるようです静的なUITableViewを使用すると、SO questionに同様の質問があり、の解像度はありません。

私は次の機能を実装することにより、セルの高さを更新するために、しかし、ことができました:

tableView.beginUpdates() 
tableView.endUpdates() 

だから、あなたが取ることができますアプローチは、次のようになります。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print(indexPath) 
    if colorCelllExpandedIndex != nil{ 
     colorCelllExpandedIndex = nil 
    } else { 
     colorCelllExpandedIndex = indexPath 
    } 
    tableView.beginUpdates() 
    tableView.endUpdates() 

} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath == colorCelllExpandedIndex { 
     return CGFloat(colorCellExpandedHeight) 
    } else { 
     if indexPath.row == 0 { 
      return CGFloat(colorCellRegularHeight) 
     } 
     return UITableViewAutomaticDimension 
    } 
} 

これはに触発されました静的なテーブルビューでセルを隠すことに関するanother SO questionの回答の1つ。

関連する問題