2

uitableviewですべての機能を選択するようなメールを作成しようとしています。同じボタンをタップすると、ユーザーはすべてのチェックマークをオンまたは削除でき、さらにユーザーは行を選択/選択解除できます(didSelectRowAtIndexPath上)。私はやろうとしましたが、正しく機能していません。これが私のコードです。

- (IBAction)selectAll:(id)sender
{
    if(myBoolean)
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];
            }
        }

        myBoolean = NO;

        [_selectUnselectButton setTitle:@"Select all Friends" forState:UIControlStateNormal];
    }
    else
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryCheckmark];
                NSLog(@"%d-%d",s,r);
            }
        }

        myBoolean = YES;

        [_selectUnselectButton setTitle:@"Unselect all Friends" forState:UIControlStateNormal];
    }
}


-(void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
4

8 に答える 8

8

あなたのニーズに合わせたサンプルコードを書きました。

基本的には

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *unifiedID = @"aCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
    }

    cell.textLabel.text = [self.states objectAtIndex:indexPath.row];

    //if the indexPath was found among the selected ones, set the checkmark on the cell
    cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *state = [self.states objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
        [self.selectedCells removeObject:indexPath];
        [self.selecedStates removeObject:state];
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        [self.selectedCells addObject:indexPath];
        [self.selecedStates addObject:state];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    NSLog(@"%@", self.selecedStates);
}


-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
    return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}


- (IBAction)selectAll:(id)sender {

    [self.selecedStates removeAllObjects];
    [self.selectedCells removeAllObjects];
    NSUInteger numberOfSections = [self.tableView numberOfSections];
    for (NSUInteger s = 0; s < numberOfSections; ++s) {
        NSUInteger numberOfRowsInSection = [self.tableView numberOfRowsInSection:s];
        for (NSUInteger r = 0; r < numberOfRowsInSection; ++r) {
            NSIndexPath *idxPath = [NSIndexPath indexPathForRow:r inSection:s];
            [self.selectedCells addObject:idxPath];
            [self.selecedStates addObject:self.states[idxPath.row]];
        }
    }
    [self.tableView reloadData];
}



- (IBAction)deselectAll:(id)sender {
    [self.selecedStates removeAllObjects];
    [self.selectedCells removeAllObjects];
    [self.tableView reloadData];
}


- (IBAction)toggleAll:(id)sender {
    if ([self.states count] == [self.selecedStates count]) {
        [sender setTitle:@"select all"];
        [self deselectAll:sender];
    } else {
        [sender setTitle:@"deselect all"];
        [self selectAll:sender];
   }
}

実際に:

ここに画像の説明を入力

あなたが呼んでいます

[[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];

tableView 内のすべてのセクションのすべての行に対して。多くの行がある場合、画面上にない行を処理するため、これは非常に非効率的です。しかし、これは必要ありません。選択したすべてのインデックス パスを配列に入れ、tableView にリロードするように指示するだけです。これにより、表示されているセルがリロードされ、セルの実装により、-tableView:cellForRowAtIndexPath:新しい行が正しく再構成されます。

于 2013-01-19T06:42:38.230 に答える
5

アクセサリビューの設定は、メソッド内で行う必要があります。tableView:cellForRowAtIndexPath:アクセサリを外部から変更する場合、外部メソッドでは、最初にモデルを変更して、特定のセルにチェックマークを付ける必要があることを示してから、を呼び出す必要がありreloadDataますUITableView

チェックされるセルを格納する1つの方法は、NSIndexSetオブジェクトの配列(セクションごとに1つのインデックスセット)です。以下の例では、単一のセクションのコードを示していますが、複数のセクションを機能させる方法を理解する必要があります。

// This variable needs to be declared in a place where your data source can get it
NSMutableIndexSet *selected;

// You need to initialize it in the designated initializer, like this:
selected = [[NSMutableIndexSet alloc] init];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    if ([selected containsIndex:indexPath.row]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    // Do the rest of your code
    return cell;
}

ここで、選択された行または選択されていない行を設定するコードで、[selected addIndex:rowToSelect]または[selected removeIndex:rowToUnselect]を呼び出し、テーブルのを呼び出す必要がありますreloadData

于 2013-01-19T05:31:03.067 に答える
3

行を選択し、行の選択を解除するために使用selectRowAtIndexPath:animated:scrollPosition:します。 詳細については、UITableViewドキュメントをお読みください
deselectRowAtIndexPath:animated:

于 2013-01-19T05:03:48.490 に答える
2

古いコードの代わりにこのコードを試してください

- (IBAction)selectAll:(id)sender
{
    if(myBoolean)
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
              [self.iTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] animated:YES scrollPosition:UITableViewScrollPositionNone];

            }
        }

        myBoolean = NO;

        [_selectUnselectButton setTitle:@"Select all Friends" forState:UIControlStateNormal];
    }
    else
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                 [self.iTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] animated:YES scrollPosition:UITableViewScrollPositionNone];
                NSLog(@"%d-%d",s,r);
            }
        }

        myBoolean = YES;

        [_selectUnselectButton setTitle:@"Unselect all Friends" forState:UIControlStateNormal];
    }
}


-(void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
于 2013-01-19T05:11:17.700 に答える
0

SelectedArrayNSMutableArrayとして別のものを取る

行内SelectedArraydidSelectRowAtIndexPathからオブジェクトを追加削除できます。

UITableViewCell呼び出しUITableViews selectRowAtIndexPathメソッドを選択できます。

[yourtable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
                       animated:NO 
                 scrollPosition:UITableViewScrollPositionTop];

selectedArrayのforループを配置して、選択したセルまたはすべてのセルにのみチェックマークを付けます。

ここで私の受け入れられた答えを確認してください:UITableViewのすべて選択ボタンを作成し、iOSの配列に選択を追加する必要があります

于 2013-01-19T06:22:27.180 に答える
0

これを実装するには、次の手順に従います。

1)mutableインデックスパスを格納する配列が必要です

2)できることは、[すべてチェック]または[すべてチェック解除]をタップするとaddremove配列への/からのすべてのインデックスパス(あなたが持っている)を実行し、更新の変更のためにテーブルをリロードすることです

3)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathデータソース メソッドで、 を使用して配列にチェックインしif([array containsObject:indexPath])、存在する場合はそれをマークするcheckedか、unchecked

4)- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPathデリゲートメソッドでは、3番目のステップで行ったように、配列にタップされたインデックスパスが既に存在することを確認し、条件に従って追加または削除 indexpathsし、更新の変更のためにテーブルをリロードする必要があります

于 2013-01-19T05:47:46.190 に答える
-1

次のようなことができます:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self doSomethingWithRowAtIndexPath:indexPath];
}
于 2013-01-19T05:07:33.990 に答える