6

背景として、この質問は私が以前に投稿した質問に関連しています:カスタムセルのUIButtonからUITableViewCellを展開/折りたたみしようとしています

要約すると、いくつかのカスタムテーブルセルを持つUITableViewがあります。各カスタムUITableViewCellにはテキスト領域があり、その後に[もっと見る]ボタンが続きます。基本的に、各セルには最初に3行のテキストが表示されますが、ユーザーが[もっと見る]ボタンをタップすると、セルはテキスト領域の高さ全体に拡大する必要があります。ボタンをもう一度タップすると、セルが折りたたまれます。

これを視覚化するのは難しいと思うので、ここで短いビデオを撮りました:http: //dl.dropbox.com/u/762437/cell-animation.mov

(これは約3.5 MBのファイルなので、ロードにそれほど時間がかかることはありません)。ビデオでは、展開/折りたたみアニメーションを実行しているセルを示しています。展開を行うと、[もっと見る]ボタンが下向きにアニメーション化されます。もう一度タップすると、アニメーションなしで元の位置にジャンプして戻ります。 編集:申し訳ありませんが、もっと明確にする必要があります。UITableViewはセルを正しくアニメーション化しています。私が求めているのは、[もっと見る]ボタンを正しくアニメーション化する方法です。

展開/折りたたみは機能していますが(正しいか間違っているかは別の問題です!)、アニメーションは期待どおりに機能していません。展開するアニメーションは機能しますが、折りたたむことはできません。

各カスタムUITableViewCellクラスには、ユーザーが[もっと見る]ボタンをタップしたときに呼び出されるIBActionがあります。また、NSMutableArrayで現在展開されているセルを追跡します。ユーザーが[閉じる]ボタンをタップすると、そのセルがアレイから削除されます。

tableViewのcellForRowAtIndexPathで、配列をチェックして、展開するセルとデフォルトサイズで表示するセルを確認します。

私はこのコードでそうしています:

// Check if the array contains the particular cell, if so, then it needs to be expanded 
if([expandedRows containsObject:indexPath])
        {
            // Expand the cell's text area to fit the entire contents
            [cell.textLabel sizeToFitFixedWidth:cell.textLabel.frame.size.width];

            // Find the new Y-position of where the "Close" button.
            // The new Y position should be at the bottom of the newly expanded text label

            CGFloat bottomYPos = cell.textLabel.frame.origin.y + cell.textLabel.frame.size.height;

            // Get a rect with the "Close" button's frame and new Y position
            CGRect buttonRect = cell.showAllButton.frame;
            buttonRect.origin.y = bottomYPos;

            // Animation block to shift the "Close" button down to its new rect             
            [UIView animateWithDuration:0.3
                                  delay:0.0
                                options: UIViewAnimationCurveLinear
                             animations:^{
                                 cell.showAllButton.frame = buttonRect;
                                 [cell.showAllButton setTitle:@"Close" forState:UIControlStateNormal];
                             } 
                             completion:^(BOOL finished){
                                 NSLog(@"Done 1!");
                             }];
        }
        else
        {
            // This cell is currently collapsed

            // Get the button rect and subtract the height delta to put it back
            // OriginalCellSizes is where I keep track of the original Y positions
            CGRect buttonRect = cell.showAllButton.frame;
            buttonRect.origin.y -= cell.frame.size.height - [[originalCellSizes objectAtIndex:indexPath.row] floatValue];

            // Animation block for the Button    
            [UIView animateWithDuration:0.3
                                  delay:0.0
                                options: UIViewAnimationCurveEaseOut
                             animations:^{
                                 cell.showAllButton.frame = buttonRect;
                                 [cell.showAllButton setTitle:@"View More" forState:UIControlStateNormal];
                             } 
                             completion:^(BOOL finished){
                                 NSLog(@"Done! 2");
                             }];
        }

調べてみると、そのif-elseの「else」ブランチで、buttonRectがすでに元の位置と同じY位置にあることがわかりました。これがアニメーションが発生しない理由だと思いますが、よくわかりません。

どんな助けでも本当に素晴らしいでしょう!

4

1 に答える 1

-2

これを行う簡単な方法......

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

insertIndexPathsは、テーブルに挿入されるNSIndexPathの配列です。

deleteIndexPathsは、テーブルから削除されるNSIndexPathの配列です。

インデックスパスの配列形式の例:

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0],
        [NSIndexPath indexPathForRow:2 inSection:0],
        nil];

この質問からそれを得ました...この質問...

于 2013-03-04T13:45:31.870 に答える