初めてセルを選択した場合、-[UITableViewDelegate tableView:didDeselectRowAtIndexPath:]
メソッドは呼び出されず、-[UITableViewDelegate tableView:didSelectRowAtIndexPath:]
が呼び出されます。もう1つのセルを選択した直後にdidDeselectRowAtIndexPath
がdidSelectRowAtIndexPath
の直後に呼び出されます。
これは問題ありません。
しかし、あなたは初めに選択したセルを表示する必要がある場合、(e.q. UITableViewCellAccessoryCheckmark
を使用して)は、その後、別のセルを選択した後、あなたはおそらく、前のセルの選択を解除するために、最初に呼び出されるdidDeselectRowAtIndexPath
方法をしたいです。
解決策!
-[UITableViewDataSource tableView:cellForRowAtIndexPath:]
の-[UITableView selectRowAtIndexPath:animated:scrollPosition:]
を呼び出して、希望するセルがすでに選択されていることを通知する必要があります。
のObjective-C
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// saving the current selected row
SelectedRow = indexPath.row;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
// preventing selection style
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = "Some text";
if (indexPath.row == SelectedRow) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// just wanted to make it selected, but it also can scroll to the selected position
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
return cell;
}
スウィフト3.1
// MARK: - UITableViewDelegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.checkmark
selectedRow = indexPath.row
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.none
}
// MARK: - UITableViewDataSource
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
// preventing selection style
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.textLabel?.text = "some text"
if (indexPath.row == selectedRow) {
cell.accessoryType = UITableViewCellAccessoryType.checkmark
// just wanted to make it selected, but it also can scroll to the selected position
tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
}
return cell
}
あなたは正しくのUITableViewのためのデリゲートを設定しましたか? –
デリゲートテーブルを指定しましたか?ユーザーがセルを選択できるようにしましたか? –
はいとはい。選択は正常に動作しますが、選択解除が問題です。 – phi