2017-02-26 13 views
0

私が達成したいのは、セルは内部のラベルをクリックすると展開/折りたたむことができます。セルのサイズは、ユーザーがセルにスクロールバックするときに使用されていたサイズになるように記憶されている必要があります。すべてのビューに高さの制約が割り当てられた状態で、自動レイアウトを使用してセルをセットアップしました。ときにラベルをユーザーがクリックすると、私は、ラベルの高さ制約を変更することにより、セルの高さを変更:最後のセルを展開/折りたたんだ後の奇妙なUITableViewの動作

func triggerExpandCollapse(_ cell: UITableViewCell) { 
    guard let cell = cell as? ActivityTableViewCell, let indexPath = self.activitiesTableView.indexPath(for: cell) else { return } 

    if !self.expandedCellIndex.insert(indexPath.row).inserted { 
     self.expandedCellIndex.remove(indexPath.row) 
     cell.descLabelHeightConstraint.constant = 60 
    } 
    else { 
     cell.descLabelHeightConstraint.constant = cell.descLabel.intrinsicContentSize.height 
    } 

    self.activitiesTableView.beginUpdates() 
    self.activitiesTableView.endUpdates() 
} 

そしてまた、すべてのセルを返す前に、セルの高さを設定:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: Constants.TableViewCellID.activity, for: indexPath) 
    guard let activityCell = cell as? ActivityTableViewCell else { 
     return cell 
    } 

    activityCell.delegate = self 
    if self.expandedCellIndex.contains(indexPath.row) { 
     activityCell.descLabelHeightConstraint.constant = activityCell.descLabel.intrinsicContentSize.height 
    } 
    else { 
     activityCell.descLabelHeightConstraint.constant = 60 
    } 
    activityCell.layoutIfNeeded() 
    return activityCell 
} 

私も自動レイアウトを有効にしますby:

self.activitiesTableView.rowHeight = UITableViewAutomaticDimension 
self.activitiesTableView.estimatedRowHeight = 1000 

これまでのところ、セルは最後のものを除いて通常のスクロール動作で拡大/縮小することができます。最後のものを展開して上にスクロールすると、activitiesTableViewは短い距離を飛び越えて見えます。詳細は、video

ここでどのようにデバッグするのか分かりません。誰もそれについていくつかのアイデアを持っていますか?ありがとう。

答えて

0

は私の心をクリアするために睡眠を持った後、私はいくつかのtryingsを持っていた/拡大アニメーションやスムーズなのUITableViewのスクロールでセルを折りたたむ持つ方法を考え出した、私は次の変更で説明することができますやっていること:

  1. セルの拡大/縮小をトリガーするアニメーションブロック内に、beginUpdatesおよびendUpdatesを含むlayoutIfNeededが含まれます。フルバージョンである:この場合

    func triggerExpandCollapse(_ cell: UITableViewCell) { 
        guard let cell = cell as? DonationsTableViewCell, let indexPath = self.donationsTableView.indexPath(for: cell) else { return } 
    
        if !self.expandedCellsSet.insert(indexPath.row).inserted // Collapse 
        { 
         self.expandedCellsSet.remove(indexPath.row) 
         cell.projectDescLabelHeightConstraint.constant = 60 
        } 
        else // Expand 
        { 
         cell.projectDescLabelHeightConstraint.constant = cell.projectDescLabel.intrinsicContentSize.height 
        } 
    
        UIView.animate(withDuration: Constants.TimeDuration.cellExpandCollapse, delay: 0, options: .curveEaseInOut, animations: { 
         cell.layoutIfNeeded() 
         self.donationsTableView.beginUpdates() 
         self.donationsTableView.endUpdates() 
        }, completion: nil) 
    } 
    
  2. 、スクロール・インジケータは、ユーザスクロールしながら、そのサイズを変更します。これは、estimatedRowHeightが実際のセルの高さと大きく異なるためです。私は各セルのおおよその高さを提供するためにtableView(_:estimatedHeightForRowAt:)を実装することを提案します。誰もがよりよい解決策を持っている場合、私は非常に感謝される返された値が大きすぎると、拡大/縮小の動作が異常

なることに注意してください!

関連する問題