Asset Type deselection


#1

I think this happens as well with the original codes in the book.

One of the asset types was selected. When I changed to another type, the tableview showed multiple selection (multiple ticks) until this view dismissed and showed again.

How can I fix this with instant selection and deselection? Thanks


#2

I’m not seeing that in my version. Are you sure you have the data model set correctly? “items” has a To-Many Relationship but “assetType” does not.


#3

@dralison

The original code in the book should perform normal that when any of the types is selected it will immediately back to the detailview.

My question is not raised against the original code but a kind of user experience which is very common. Single selection amongst tableviewcells with instant selection change.
Using the original code for this purpose, that is by disabling the auto-returning back to detailview, the table would show multiple selection if you try to select different cells. It will only become single when you dismiss that tableview and back to it.


#4

I’m seeing the same thing. Is it possible to dismiss a check mark immediately upon the selection of a different check mark?


#5

Hm… not 100% sure what you are asking, but try sending deselectedRowAtIndexPath:animated: to the table view or reloading the table?


#6

This fixed the problem for me.

[[self tableView] reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:NO];

Here are my two methods in AssetTypePicker.m that deal with checkmarks (note: I am in the middle of the Bronze challenge, so I haven’t quite implemented everything, but the code dealing with setting/removing checkmarks works).

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"UITableViewCell"];
    }
    
    NSArray *allAssets = [[BNRItemStore sharedStore] allAssetTypes];
    NSManagedObject *assetType = [allAssets objectAtIndex:[indexPath row]];
    
    // Use key-value coding to get the asset type's label
    NSString *assetLabel = [assetType valueForKey:@"label"];
    [[cell textLabel] setText:assetLabel];
    
    // Checkmark the one that is currently selected
    if (assetType == [item assetType]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    
    NSArray *allAssets = [[BNRItemStore sharedStore] allAssetTypes];
    NSManagedObject *assetType = [allAssets objectAtIndex:[indexPath row]];
    [item setAssetType:assetType];
    
    [[self tableView] reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:NO];
        
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        //TODO
    } else {
        [[self navigationController] popViewControllerAnimated:YES];
    }
    
}