2

多数のセルを含むテーブル ビューがあります。(モーダル ビュー コントローラーを使用して) 新しいセルを追加すると、新しく追加されたセルをユーザーに表示したいと思います。これを行うには、テーブル ビューを新しいセルまでスクロールし、選択してすぐに選択解除します。

現在、deselectRowAtIndexPath一定の間隔を置いて、テーブル ビューに を送信しています。

- (IBAction)selectRow 
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    [self performSelector:@selector(deselectRow:) withObject:indexPath afterDelay:1.0f];
}

- (void)deselectRow:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

これを行うためのより良い方法があるかどうか疑問に思っています。それはうまく機能しますが、静的タイマーに依存して、時々異なる時間がかかる操作を実行するのは好きではありません (たとえば、テーブルが非常に長い場合)。

編集:デリゲートメソッドが起動されselectRowAtIndexPath:animated:scrollPositionないことに注意してください。UITableViewどちらtableView:didSelectRowAtIndexPath:scrollViewDidEndDecelerating:呼び出されません。ドキュメントから:

このメソッドを呼び出しても、デリゲートはtableView:willSelectRowAtIndexPath:またはtableView:didSelectRowAtIndexPath:メッセージを受信したり、UITableViewSelectionDidChangeNotificationオブザーバーに通知を送信したりしません。

4

1 に答える 1

0

UITableViewDelegateの拡張ですUIScrollViewDelegate。メソッドの 1 つを実装し、UIScrollViewDelegateそれを使用して行の選択を解除するタイミングを決定できます。scrollViewDidEndDecelerating:開始するのに適した場所のようです。

また、私は個人的performSelector...に、1つのパラメーターの制限によりメソッドが制限されていることを発見しました。私はGCDを使用することを好みます。コードは次のようになります。

- (IBAction)selectRow 
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    //deselect the row after a delay
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
});

}

于 2012-05-11T10:41:50.840 に答える