2

セクションと複数選択のあるテーブルビューを使用していますが、1つの行が選択されたときに複数の行がチェックされるという問題があります...これに関する他のスレッドをいくつか見ましたが、実際には解決策が得られませんでした。 ..

これが私のコードです:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{    
    [employeeTable deselectRowAtIndexPath:[employeeTable indexPathForSelectedRow] animated:NO];

    UITableViewCell *cell = [employeeTable cellForRowAtIndexPath:indexPath];    

    // get the letter in each section
    NSString *alphabet = [charIndex objectAtIndex:indexPath.section];

    // get the names beginning with the letter
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];    

    NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate];    

    NSString *name = [names objectAtIndex:indexPath.row];

    for(int i = 0; i < [employeeConnection.employees count]; i++)
    {
        Employee *aEmployee = [employeeConnection.employees objectAtIndex:i];

        NSString *firstName = aEmployee.firstName;
        NSString *lastName = aEmployee.lastName;
        NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

        if([fullName isEqualToString:name])
        { 
            NSLog(@"Name: %@", name);

            if (cell.accessoryType == UITableViewCellAccessoryNone) {

                cell.accessoryType = UITableViewCellAccessoryCheckmark;

                // Reflect selection in data model
                [chosenEmployees addObject:aEmployee.employeeID];
                [chosenEmployeesNames addObject:name];

            } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {

                cell.accessoryType = UITableViewCellAccessoryNone;

                // Reflect deselection in data model
                [chosenEmployees removeObject:aEmployee.employeeID];
                [chosenEmployeesNames removeObject:name];
            }
        }
    }
}

更新:cellForRowAtIndexPathを追加しました

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        cell.textLabel.textColor = [UIColor whiteColor];
    }

    // Get the letter in the current section
    NSString *alphabet = [charIndex objectAtIndex:[indexPath section]];

    // Get the names beginning with the letter
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
    NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate];

    if([names count] > 0)
    {
        // Extract the name
        cell.textLabel.text = [names objectAtIndex:indexPath.row];
    }

    return cell;
}
4

2 に答える 2

10

NSMutableSetチェックされたManagedObject(CoreDataを使用している場合)または単にチェックされたIndexPathのいずれかを保存することをお勧めします。-cellForRowAtIndexPathの場合:セルがチェックされることになっているのかどうかをチェックできます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *const identifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.textLabel.textColor = UIColor.whiteColor;
    }

    if ([self.checkedIndexPaths containsObject:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *const cell = [tableView cellForRowAtIndexPath:indexPath];
    [table deselectRowAtIndexPath:indexPath animated:NO];

    if ([self.checkedIndexPaths containsObject:indexPath]) {
        [self.checkedIndexPaths removeObject:indexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else {
        [self.checkedIndexPaths addObject:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
于 2012-04-18T20:18:53.333 に答える
0

セルは再利用されているため、cellForRowAtInexPathテーブルデータソースメソッドのテーブル内のすべてのセルに対してアクセサリマークをオンまたはオフに設定する必要があります。

したがって、cell.accessoryTypeセルプロパティは、didSelectRowデリゲートメソッドではなく、cellForRowAtInexPathで指定する必要があります。

didSelectRowで、配列内の選択された行を追跡し、配列の値に応じて、セルのアクセサリマークをnoneに設定するか、cellForRowAtInexPathでチェックマークを設定します。

于 2012-04-18T20:21:40.003 に答える