テーブルビューを作成し、データを表示しました。テーブルビューでデータをクリックすると、アクセサリマークが付けられます(UITableViewCellAccessoryCheckmark、Like、Check Markを使用)。ここで、インデックス位置の状態を保持したいと思います。別のクラスに行ってからテーブルビューに戻ると、前の状態(アクセサリマーク)が表示されるためです。では、状態を保持するにはどうすればよいですか。つまり、インデックスパス値を保存または保存できます(AppDelegateメソッドを使用せずに)。これを実現するにはどうすればよいですか?
ここに私のサンプルコードは、
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
checkedData];
oldCell.accessoryType = UITableViewCellAccessoryNone;
checkedData = indexPath;
}
if (newRow == oldRow) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
// cell.accessoryType = UITableViewCellAccessoryNone;
}
checkedData = indexPath;
}
}
テーブルビュークラスに戻るときは、前の状態を保持する必要があります。では、どうすればそれにアクセスできますか?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(checkedData == indexPath) // Doesn't works
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
私を助けてください。
ありがとう