Search code examples
iosxcodecore-datadidselectrowatindexpath

make "Next" Button disabled if no cell is checked


THX FOR THE HELP!

I changed the Viewdidload like that and it is working fine for me now! Iam Fetching the location and took a predicate to synchronize the checks and the state of the button

  - (void)viewDidLoad
  {
    [super viewDidLoad];
    app = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [app managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Standort" inManagedObjectContext:context];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ortcheck CONTAINS YES"];
    [request setPredicate:predicate];
    [request setEntity:entity];

    NSError *error = nil;
    NSArray *events = [context executeFetchRequest:request error:&error];    
    int i = events.count;

    if (i == 0) {

        myWeiter = [[UIBarButtonItem alloc] initWithTitle:@"Weiter" style:UIBarButtonItemStyleBordered target:self action:@selector(nextPressed:)];

        self.navigationItem.rightBarButtonItem = myWeiter;


         self.navigationItem.rightBarButtonItem.enabled = NO;
    }
    else {
        myWeiter = [[UIBarButtonItem alloc] initWithTitle:@"Weiter" style:UIBarButtonItemStyleBordered target:self action:@selector(nextPressed:)];

        self.navigationItem.rightBarButtonItem = myWeiter;


        self.navigationItem.rightBarButtonItem.enabled = YES;
    }

Solution

  • Add a check counter as an instance member, and initialize it to 0, on each check increase it, and on each uncheck decrease it, after each check set the button:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // snip
    
        //if (cell.checkButton.hidden==YES){
        //    cell.checkButton.hidden=NO;
        //}else {
        //    cell.checkButton.hidden=YES;
        //}
        BOOL state = !cell.checkButton.hidden;
        cell.checkButton.hidden=state; // simpler
        self.counter += (state) ? 1 : -1;
        [nextButton setEnabled: counter > 0];
    
        // snap
    }