私はUITableViewCell
を使用したカスタムUITableView
を持っています。
各UITableViewCell
には2つのボタンがある。これらのボタンをクリックすると、セル内の UIImageView
にある画像が変更される。
それぞれのセルを別々にリフレッシュして新しい画像を表示することは可能でしょうか? どんなヘルプでも結構です。
セルのindexPathを取得したら、次のようにすることができる:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Xcode 4.6以降では
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
もちろん、アニメーションのエフェクトは自由に設定できます。
UITableViewのcellForRowAtIndexPath:]を呼んでみましたが、うまくいきませんでした。しかし、例えば以下のようにするとうまくいく。NSArray
を alloc
と release
してメモリ管理を徹底している。
- (void)reloadRow0Section0 {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[indexPaths release];
}
スウィフト:
func updateCell(path:Int){
let indexPath = NSIndexPath(forRow: path, inSection: 1)
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) //try other animations
tableView.endUpdates()
}
reloadRowsAtIndexPaths:
は問題ありませんが、それでも UITableViewDelegate
メソッドを強制起動します。
私が想像できる最も簡単なアプローチは:です。
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self configureCell:cell forIndexPath:indexPath];
メインスレッドで configureCell:
実装を呼び出すことは重要です 非UIスレッドでは機能しません( reloadData
/ reloadRowsAtIndexPaths:
と同じストーリー)。 追加すると役立つ場合があります。
dispatch_async(dispatch_get_main_queue(), ^
{
[self configureCell:cell forIndexPath:indexPath];
});
また、現在表示されているビュー以外で行われる作業を回避する価値もあります。
BOOL cellIsVisible = [[self.tableView indexPathsForVisibleRows] indexOfObject:indexPath] != NSNotFound;
if (cellIsVisible)
{
....
}
カスタムTableViewCellsを使用している場合は、ジェネリックです。
[self.tableView reloadData];
現在のビューを離れない限り、この質問に効果的に答えません**戻ってきてください。 最初の答えもしません。
ビューを切り替えることなく最初のテーブルビューセルを正常にリロードするには、次のコードを使用します。
//For iOS 5 and later
- (void)reloadTopCell {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}
上記のメソッドを呼び出す次の更新メソッドを挿入して、トップセル(または必要に応じてテーブルビュー全体)のみをカスタムリロードできるようにします。
- (void)refresh:(UIRefreshControl *)refreshControl {
//call to the method which will perform the function
[self reloadTopCell];
//finish refreshing
[refreshControl endRefreshing];
}
これでソートしたので、 viewDidLoad
内に以下を追加します。
//refresh table view
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refreshControl];
これで、トップセルをリロードするカスタム更新テーブル機能があります。 テーブル全体をリロードするには、を追加します。
[self.tableView reloadData];
新しい更新方法に。
ビューを切り替えるたびにデータをリロードする場合は、次の方法を実装します。
//ensure that it reloads the table view data when switching to this view
- (void) viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
}
iOS 6の新しいリテラル構文で、これらの答えを少し更新しておきます--単一のオブジェクトにはPaths = @[indexPath]を、複数のオブジェクトにはPaths = @[indexPath1, indexPath2,...]を使うことができます。
個人的には、配列や辞書のリテラル構文は非常に便利で時間の節約になります。 ひとつは読みやすいということ。 また、複数のオブジェクトを持つリストの最後にnilを書く必要がなくなる。 我々は皆、傾けるべき風車を持っている。)
ただ、これをミックスに投げ込んでみようと思ったんだ。 お役に立てれば幸いです。
アップグレードセルが必要ですが、キーボードを閉じます。 使用する場合。
let indexPath = NSIndexPath(forRow: path, inSection: 1)
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) //try other animations
tableView.endUpdates()
キーボードが消えます。
Swift 5のUITableView拡張機能は次のとおりです。
import UIKit
extension UITableView
{
func updateRow(row: Int, section: Int = 0)
{
let indexPath = IndexPath(row: row, section: section)
self.beginUpdates()
self.reloadRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.automatic)
self.endUpdates()
}
}
呼び出します。
self.tableView.updateRow(row: 1)