Domanda

Ho una visione di tabella contenente un elenco di nomi utente che sono indicizzati in sezioni e righe in ordine alfabetico. Quando tocco una delle righe in una sezione, l'utente corretto viene aggiunto al mio array di destinatari e un segno di spunta è positivo nella cella oltre al loro nome .. ma un segno di spunta viene anche visualizzato accanto ad altri nomi utente che non sono stati selezionati e non sono nell'array dei destinatari. Ho provato a riassegnare la cella selezionata con un nuovo indice (vedi codice sotto) ma non sono stato in grado di farlo funzionare. Registra il percorso corretto, ma non lo assegnerà. Sto usando un metodo simile per assegnare agli utenti le righe in ogni sezione senza problemi, ma per qualche motivo i marchi accessori mi danno problemi. Ho visto alcuni altri thread su Overflow su questo stesso argomento, ma Wash; 'Terba di arrivare a una soluzione per il mio caso. Qualche indizio? Saluti!

 - (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]);
        }
    }];

Ed ecco il cellfrowortIndexPath:

- (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;
}
È stato utile?

Soluzione

Come vedo che hai commesso 2 errori in cellfroworindexpath che non hanno verificato se la cella è nulla per creare uno e impostare accessori per celle secondo l'elenco dei destinatari.

Dovresti fare di seguito:

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top