1

複数のセクションを持つグループ化されたテーブルビューを使用しています。そして、indexpath メソッドで didselectrow に複数選択の機能を実装する必要があります。私のコードは次のとおりです。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
} 

これにより、複数のセルを選択できますが、そのときにテーブルビューをスクロールすると、選択が消えます。

4

4 に答える 4

1

cellForRowAtIndexPath を呼び出し、選択を処理していないため、スクロールすると選択がオフになります。

この問題を回避するには、次のようにします。 didSelectRowAtIndexPath では、選択した行のインデックス パスを次のように保存できます。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        //remove index path
        [selectedIndexPathArray removeObject:path];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [selectedIndexPathArray addObject:path];
    }
}

セルが選択されcellForRowAtIndexPathているかどうかを確認できます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //If selectedIndexPathArray contains current index path then display checkmark.
    if([selectedIndexPathArray containsObject:indexPath])
       cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
于 2012-07-10T11:33:49.877 に答える
0

スクロール時にメソッドCellForRowAtIndexPathが呼び出されるため、選択が消えます。アクセサリを再度設定する必要があります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    // here
    ...
}
于 2012-07-10T11:21:02.123 に答える
0

行の選択を追跡していないため、この問題が発生しています。コールバック メソッドcellForRowAtIndexPathが消えた/(上/下にスクロールされた) 行に対して呼び出されると、セル オブジェクトは選択されたかどうかを記憶しなくなります。(それらの選択が消えている理由)

選択した行を追跡するには、NSMutableArray/NSArrayなどのコレクションを使用することをお勧めします。これらのアプローチのいずれかを使用できます。

これは簡単な作業修正です。ユーザーの選択に基づいてdidSelectRowAtIndexPathのインデックス パス オブジェクトを追加/削除し、その配列の内容に基づいて、対応するセルの cell.accessoryType の値を切り替えることができます。

理想的には、 selectedと呼ばれるブール値メンバーを持つデータ Bean/モデルを使用でき、選択内容に基づいてその値を更新できます。次に、これらのインデックス パスを単純に追加する代わりに、これらの意味のあるデータ Bean オブジェクトを配列に追加して、Beanの選択されたプロパティから選択戻す.ケースと要件!)

これが役に立ったことを願っています!

于 2012-07-10T13:43:56.157 に答える
0

これを試して

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    if (self.tableView.isEditing) {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    return cell;
}

-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleMultiSelect;
}

-(IBAction) switchEditing {
    [self.tableView setEditing:![self.tableView isEditing]];
    [self.tableView reloadData]; // force reload to reset selection style
}

これが問題の解決に役立つことを願っています.( ref )

于 2012-07-10T11:18:44.083 に答える