1

右スワイプで UITableViewCell の背景画像を切り替えています。問題は、追加[taskManager reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];すると一度だけトグルされ、その後トグルされないことです。では、これはなぜですか?これが起こらない理由は何ですか?これがメソッドコード全体です。

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer {
CGPoint location = [gestureRecognizer locationInView:self.taskManager];

NSIndexPath *indexPath = [self.taskManager indexPathForRowAtPoint:location];

[taskManager reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

UIImage *notCkDone = [UIImage imageNamed:@"UITableViewCellCheckmark"];
UIImage *isCkDone = [UIImage imageNamed:@"UITableViewCellCheckmarkDone"];

UIImage *notBgDone = [UIImage imageNamed:@"UITableViewCell"];
UIImage *isBgDone = [UIImage imageNamed:@"UITableViewCellDone"];

UITableViewCell *cell = [taskManager cellForRowAtIndexPath:indexPath];

if (indexPath) {
    if (cell.imageView.image == notCkDone) {
        cell.imageView.image = isCkDone;
        cell.backgroundView = [[UIImageView alloc] initWithImage:isBgDone];
    } else {
        cell.imageView.image = notCkDone;
        cell.backgroundView = [[UIImageView alloc] initWithImage:notBgDone];
    }
    cell.textLabel.textColor = [UIColor colorWithRed:0.0/0.0 green:0.0/0.0 blue:0.0/0.0 alpha:1.0];
}

}

4

1 に答える 1

3

セルのビジュアルに対するすべての変更は、tableView:cellForRowAtIndexPath:. ジェスチャ レコグナイザーのイベント ハンドラー (または 以外の場所) でそれを行うべきではありませんtableView:cellForRowAtIndexPath

ジェスチャレコグナイザーは、テーブル ビューの背後にあるモデルを変更する必要があります。これにより、tableView:cellForRowAtIndexPath はnotCkDoneisCkDone次回のデータのリロード時に画像を設定する決定を下すことができ、背景の変更にも対処できます。その後、ジェスチャ レコグナイザーはテーブル ビューにデータをリロードするよう指示する必要があります。

現在のソリューションは、によって返されたセルで動作しtableView:cellForRowAtIndexPathます。外部からビジュアルを変更すると、セルが画面に表示されている間にテーブル ビューに変更が表示されます。ただし、セルが画面外にスクロールすると、利用されます。つまり、次にセル識別子を使用してセルをデキューすると、以前は別のセルとして表示されていたオブジェクトが取得される可能性があります。

于 2013-01-25T00:35:10.433 に答える