何時間も費やして何の役にも立ちませんでした。検索(グーグル)がうまくいかなかったのか、それとも質問が少ないのか、専門家の回答を実装する際に間違いを犯したのかわかりません。
セクション内の 1 つの行にアクセサリ タイプのチェック マークを設定し、他の行には何も設定しないといういくつかの質問があることは知っています。
テーブル ビューに 2 つのセクションがあります。デフォルトでは、各セクションの 1 番目の行を選択します。つまり、アクセサリ ビューのチェック マークを付けます。ここから、ユーザーが行を選択すると、選択した行にチェック マークが表示されるようにします。各セクションで行の選択を追跡するために、2 つのインデックス パスを宣言しようとしました。実装コードは次のとおりです。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.row,indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (indexPath.section == 0)
{
if(self.firstSectionIndex == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
if (indexPath.section == 1)
{
if(self.secondSectionIndex == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
switch (indexPath.section)
{
case 0:
{
if (indexPath.row == 0)
{
if(self.firstSectionIndex != indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = @"Yes";
}
if (indexPath.row == 1)
{
cell.textLabel.text = @"No";
}
}
break;
case 1:
{
if (indexPath.row == 0)
{
if(self.secondSectionIndex != indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = @"Easy";
}
if (indexPath.row == 1)
{
cell.textLabel.text = @"Medium";
}
if (indexPath.row == 2)
{
cell.textLabel.text = @"Hard";
}
}
break;
default:
break;
}
}
tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;
return cell;
}
私の didSelectRowAtIndexPath では、次のことを行いました。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
self.firstSectionIndex = indexPath;
}
if (indexPath.section == 1)
{
self.secondSectionIndex = indexPath;
}
[tableView reloadData];
}
長期参照用に保存するセル選択の状態を検索しましたが、それを求めて、便利なリンクhereを見つけました。
しかし、複数のセルを選択しており、セクションのすべての行にアクセサリ タイプのチェック マークが適用されています。何が問題なのかわかりません。どなたか教えてください!!
よろしくお願いします:)