UITableViewCellの更新について

UITableViewCell内で受け取った値の更新をしようとしたら嵌ったのでメモ。

cellだけ更新したい場合は、reloadRowsAtIndexPaths:withRowAnimation:でcellを指定して

UITableView* myTableView = テーブルビュー;
for (int i = 行数 - 1; i >= 0; --i) {
	if (何らかの条件) {
		NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:0];
		NSArray* indexPaths = [NSArray arrayWithObject:indexPath];
		[myTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
	}
}
あかばね式 [iPhone]UITableViewで1行単位で行のリフレッシュを行う方法


というような感じでできる…はずがcellの値は更新されないし、画面遷移しない状況で使うのはまずいのか表示が崩れた。
ということで調べた結果、setNeedsLayoutに変更。

(中略)
[headingCell setNeedsLayout];
特定のUITableViewCellだけ再描画 - nkmrshnの日記


表示が崩れることはなくなったけど、相変わらず更新がかからない…
NSLogでは受け取れているのが確認できるし、実際cellを一度画面外に追いやって再描画させると出てくる。
ここでしばらく嵌ること小一時間。
performSelectorOnMainThread:withObject:waitUntilDone:にsetNeedsLayoutを突っ込むことで解決。

データの更新などを行った後、スレッド内で単に
[(UITableView)tableView reloadData]
とやると落ちてしまう。そこで以下のようなコードを記述することによって、データの再読み込み処理をメインスレッドに戻して処理させるようにする。
[(UITableView)tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
http://ebisu.surbiton.jp/sysblog/2012/04/uitableview.html


先人に感謝。