特定のUITableViewCellだけ再描画

方位を取得(startUpdatingHeading)し、didUpdateHeadingが呼ばれたら、UITableViewCellのdetailTextLabel.textを書き換えようと思い、次のように書きました。

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
 NSString *heading = [NSString stringWithFormat:@"%.0f", newHeading.trueHeading];

 UITableView *tableView = (UITableView *)self.view;
 UITableViewCell *headingCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:kHeading inSection:kLocationSection]];

 headingCell.detailTextLabel.text = heading;
}

このままでも動き、方位も一応は表示されました。

しかし、例えば最初2桁の方位で(1)、次に3桁の方位に向いた時は何も表示されなくなりました(2)。指でテーブルを動かしてcellを再描画させれば、きちんと3桁が表示されるのですが。

detailTextLabel.textにheading(方位)を代入した後、[tableView reloadData]しようかと思ったのですが、これだと方位が1度変化しただけでテーブル全体を更新してしまう。テーブルには他のセルもあるし、スクロールの位置も変わってしまうので、これは何とかして避けたい。セルだけ再描画できないものだろうかと...

散々、ネットを検索した結果、文字数が同じあるいは減るなら必要は無いのですが、増える場合はUITableViewCellの再描画を指定(setNeedsLayout)してやれば、方位が2桁から3桁に変化しても表示されるようになりました(3)。

 headingCell.detailTextLabel.text = heading;
 [headingCell setNeedsLayout]
}