Pergunta

Eu tenho uma TableView contendo uma lista de nomes de usuário indexados em seções e linhas em ordem alfabética. Quando eu tiro em uma das linhas em uma seção, o usuário correto é adicionado à minha matriz de destinatários e uma marca de seleção é lugar na célula além do nome. Mas uma marca de seleção também é exibida ao lado de outros nomes de usuário que não foram selecionados e não estão na matriz de destinatários. Tentei reatribuir a célula selecionada com um novo indexPath (veja o código abaixo), mas não consegui fazê -lo funcionar. Ele registra o caminho correto, mas não o atribui. Estou usando um método semelhante para atribuir aos usuários as linhas em cada seção sem problemas, mas por algum motivo as marcas de acessórios estão me dando problemas. Eu já vi alguns outros threads sobre o transbordamento sobre esse mesmo tópico, mas lavarem; 'Tabela de chegar a uma solução para o meu caso. Alguma pista? Felicidades!

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    int row = indexPath.row;
    int section = indexPath.section;
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];

    [tableView deselectRowAtIndexPath:newIndexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];

    NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
    PFUser *user = [array objectAtIndex:indexPath.row];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.recipients addObject:user];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.recipients removeObject:user];
    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
    }];

E aqui está o CellFororrowatIndexpath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Get the user names from the array associated with the section index in the sections array.
    NSArray *userNamesInSection = (self.sectionsArray)[indexPath.section];

    // Configure the cell with user name.
    UserNameWrapper *userName = userNamesInSection[indexPath.row];
    cell.textLabel.text = userName.user;

    return cell;
}
Foi útil?

Solução

Como eu vejo, você cometeu 2 erros no CellForgunderindexpath, que não verificaram se a célula é nula para criar um e definir o acessório para célula de acordo com a lista de destinatários.

Você deve fazer como abaixo:

NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

PFUser *user = [self getUserAtIndexPath:indexPath];
cell.textLabel.text = user.name;

if ([self.recipients containObject:user]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top