0

This is updated post.

i have one Viewcontroller with tableview,check button (left side). when i click my check button in table view its working well for check/uncheck.its working fine.

Needed:

Inspite of clicking the check button in my table view. i need to click my cell row,at that time the particular selected cell row check button should check/uncheck. how to that i have tried all way . but no solution got .Here this is my viewcontroller.m file:

Thanks in advance !

@interface ViewController ()
{

//    UILabel *textLabel;
//    UILabel *detailLabel;
    NSDateFormatter *formatter;
}



@property (strong) NSMutableArray *notes;
@end

@implementation ViewController
@synthesize tableView;
@synthesize addButton;
@synthesize catefetchedResultsController,filteredTableData,searchSTR;


- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}



- (AppDelegate *)appDelegate
{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}


#pragma mark -  fetching
- (NSFetchedResultsController *)fetchedResultsControllerCate
{
    if (catefetchedResultsController == nil)
    {




        NSManagedObjectContext* mangedobjectContext=[self appDelegate].managedObjectContext;

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Notes"
                                                  inManagedObjectContext:mangedobjectContext];
        NSFetchRequest *request= [[NSFetchRequest alloc] init];

        NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"mod_time" ascending:YES];
        NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];

        NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1,sd2, nil];




        [request setSortDescriptors:sortDescriptors];
        [request setEntity:entity];






        NSPredicate *predicate;


        if (self.searchSTR.length > 0) {
            predicate=nil;



                NSPredicate *predicate =[NSPredicate predicateWithFormat:@"(note CONTAINS[c] %@)",self.searchSTR]; //[NSPredicate predicateWithFormat:@"name LIKE %@",searchResults];
                [request setPredicate:predicate];

        }else{


        }

        [request setFetchBatchSize:10];

        catefetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                           managedObjectContext:mangedobjectContext
                                                                             sectionNameKeyPath:nil
                                                                                      cacheName:nil];

        [catefetchedResultsController setDelegate:self];


        NSError *error = nil;
        if (![catefetchedResultsController performFetch:&error])
        {
            NSLog(@"Error performing fetch: %@", error);
        }
    }
  //  NSLog(@"inside fetch %@",self.notes);
    return catefetchedResultsController;

}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    if (controller==catefetchedResultsController)
    {
        [self.tableView reloadData];
      //  NSLog(@"inside");
    }

}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if(textField == self.searchCate){

     //   [self filterCate:searchStr];


        self.searchSTR=searchStr;
        catefetchedResultsController=nil;
        [self catefetchedResultsController];
        [self.tableView reloadData];
    }
    return true;
}


-(void)filterCate:(NSString*)text
{
    filteredTableData = [[NSMutableArray alloc] init];

    // Create our fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    // Define the entity we are looking for

    NSManagedObjectContext* mangedobjectContext=[self managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Notes"
                                              inManagedObjectContext:mangedobjectContext];


    [fetchRequest setEntity:entity];
    NSPredicate *predicate;// = [NSPredicate predicateWithFormat:@"mod_time==%i",0];
   // [fetchRequest setPredicate:predicate];


    NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"mod_time" ascending:YES];
    NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1,sd2, nil];
      [fetchRequest setSortDescriptors:sortDescriptors];

    // If we are searching for anything...
    if(text.length > 0)
    {
        // Define how we want our entities to be filtered
        predicate = [NSPredicate predicateWithFormat:@"(title CONTAINS[c] %@)", text];
        [fetchRequest setPredicate:predicate];
    }
    NSError *error;

    // Finally, perform the load
    NSArray* loadedEntities = [mangedobjectContext executeFetchRequest:fetchRequest error:&error];
    filteredTableData = [[NSMutableArray alloc] initWithArray:loadedEntities];

    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"My Notes";

    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];

    formatter = [[NSDateFormatter alloc] init];
    formatter.doesRelativeDateFormatting = YES;
    formatter.locale = [NSLocale currentLocale];
    formatter.dateStyle = NSDateFormatterShortStyle;
    formatter.timeStyle = NSDateFormatterNoStyle;
                CATransition *animation = [CATransition animation];
            [animation setDuration:2.0];
            [animation setType:kCATransitionPush];
        [animation setSubtype:kCATransitionFromTop];

        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

        [[addButton layer] addAnimation:animation forKey:@"SwitchToDown"];


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];





}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

   //  Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Notes"];

    NSError *error = nil;
    self.notes = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];

    NSSortDescriptor *titleSorter= [[NSSortDescriptor alloc] initWithKey:@"mod_time" ascending:NO];

    [self.notes sortUsingDescriptors:[NSArray arrayWithObject:titleSorter]]
    ;

    NSLog(@"Your Error - %@",error.description);

    [tableView reloadData];
}

-(void)dismissKeyboard {
    [_searchCate resignFirstResponder];
}
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//
//}

//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [[[self fetchedResultsControllerCate] sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    // Return the number of rows in the section.
   // return self.notes.count;


    NSArray *sectionCate = [[self fetchedResultsControllerCate] sections];





        if (sectionIndex < [sectionCate count])
        {
            id <NSFetchedResultsSectionInfo> sectionInfo = [sectionCate objectAtIndex:sectionIndex];

           // NSLog(@"sectionInfo.numberOfObjects %lu",(unsigned long)sectionInfo.numberOfObjects);

            return sectionInfo.numberOfObjects;

        }


    return 0;


}





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


        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];


    cell.selectionStyle = UITableViewCellSelectionStyleNone;

      FilterButton *testButton = [[FilterButton alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
    UIImageView*sideImage=[[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];

    // Configure the cell...
    Notes*note=(Notes*)[[self fetchedResultsControllerCate] objectAtIndexPath:indexPath];

    NSDate *date =note.mod_time;

        NSString *dateString = [formatter stringFromDate:date];
    if([note.check isEqualToString:@"yes"]){
        cell.backgroundColor = [UIColor colorWithRed:230/255.0 green:231/255.0 blue:230/255.0 alpha:1];

        // cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]]; //set image for cell 0

        [sideImage setImage:[UIImage imageNamed:@"tick"]];

    }else{
        cell.backgroundColor = [UIColor clearColor];


   // [testButton setImage:[UIImage imageNamed:@"oval"] forState:UIControlStateNormal];

        [sideImage setImage:[UIImage imageNamed:@"oval"]];
    // NSLog(@" write no");
    }

     testButton.myDate=note.mod_time;
    cell.textLabel.text = note.title;

    cell.detailTextLabel.text = dateString;
     [testButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];

     [cell setIndentationLevel:1];
    [cell setIndentationWidth:45];
    [cell.contentView addSubview:sideImage];
    [cell.contentView addSubview:testButton];



    return cell;

}



-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [textField resignFirstResponder];


    return YES;
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell.textLabel.font = [UIFont fontNamesForFamilyName:@"Avenir"];
    cell.textLabel.font = [UIFont fontWithName:@"Avenir" size:19.0];


    cell.detailTextLabel.font=[UIFont fontWithName:@"Avenir" size:15.0];

}

-(void)buttonTouched:(id)sender

{
        FilterButton *btn = (FilterButton *)sender;


    NSManagedObjectContext *context = [self managedObjectContext];
    Notes * NotesUpdateing;

    NSFetchRequest *request= [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Notes" inManagedObjectContext:context];
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"mod_time==%@",btn.myDate];
   // NSLog(@"btn.myDate   ..%@",btn.myDate);
    [request setEntity:entity];
    [request setPredicate:predicate];


    NSError *error = nil;

    // Below line is giving me error

    NSArray *array = [context executeFetchRequest:request error:&error];

    if (array != nil) {
        NSUInteger count = [array count]; // may be 0 if the object has been deleted.
        if(count==0){

            NSLog(@"nothing  to updates");



        }else{
           NotesUpdateing = (Notes*)[array objectAtIndex:0];

            if ([NotesUpdateing.check isEqualToString:@"no"]) {


            NotesUpdateing.check=@"yes";
             NSLog(@" write yes");
                btn.selected=NO;

            }
            else
            {
                NotesUpdateing.check=@"no";
                btn.selected=NO;
                 NSLog(@" write no");

            }

        }
    }
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
    [self.tableView reloadData];






    }


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

    Notes *note = (Notes *)[[self fetchedResultsControllerCate] objectAtIndexPath:indexPath];

    note.check = [note.check isEqualToString:@"yes"] ? @"no" : @"yes";

    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];
}





- (IBAction)addButtonPressed:(id)sender {
    AddNoteViewController *addNoteVC = [AddNoteViewController new];




    // to remove unused warning....
#pragma unused (addNoteVC)

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



- (void)tableView:(UITableView *)cTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   // NSManagedObjectContext *context = [self managedObjectContext];






    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete object from database


        NSError *error = nil;




         Notes*note=(Notes*)[[self fetchedResultsControllerCate] objectAtIndexPath:indexPath];


        NSManagedObjectContext *context = [self managedObjectContext];


        NSFetchRequest *request= [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Notes" inManagedObjectContext:context];
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"mod_time==%@",note.mod_time];
        // NSLog(@"btn.myDate   ..%@",btn.myDate);
        [request setEntity:entity];
        [request setPredicate:predicate];



        // Below line is giving me error

        NSArray *array = [context executeFetchRequest:request error:&error];

        if (array != nil) {
            NSUInteger count = [array count]; // may be 0 if the object has been deleted.
            if(count==0){

                NSLog(@"nothing  to updates");



            }else{


                [context deleteObject:[array objectAtIndex:0]];

            }
        }



        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }

    }
}
- (IBAction)btnClick:(id)sender {
}



-(UITableViewCell*)CategoryTablecreateCellFor:(UITableViewCell*)cell CellindexPath:(NSIndexPath*)indexPath{
    Notes*cateRecipe=(Notes*)[[self fetchedResultsControllerCate] objectAtIndexPath:indexPath];


    UILabel *txt=[[UILabel alloc] initWithFrame:CGRectMake(130, 0, 150, 70)];
    txt.text=cateRecipe.title;
    txt.textColor=[UIColor darkGrayColor];
    [txt setFont: [UIFont fontWithName:@"Helvetica" size:15.0]];




    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    [cell.contentView addSubview:txt];


    return cell;
}
@end

This is my cellForRowAtIndexPath :

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


        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];


    cell.selectionStyle = UITableViewCellSelectionStyleNone;

      FilterButton *testButton = [[FilterButton alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
    UIImageView*sideImage=[[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];

    // Configure the cell...
    Notes*note=(Notes*)[[self fetchedResultsControllerCate] objectAtIndexPath:indexPath];

    NSDate *date =note.mod_time;

        NSString *dateString = [formatter stringFromDate:date];
    if([note.check isEqualToString:@"yes"]){
        cell.backgroundColor = [UIColor colorWithRed:230/255.0 green:231/255.0 blue:230/255.0 alpha:1];

        // cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]]; //set image for cell 0

        [sideImage setImage:[UIImage imageNamed:@"tick"]];

    }else{
        cell.backgroundColor = [UIColor clearColor];


   // [testButton setImage:[UIImage imageNamed:@"oval"] forState:UIControlStateNormal];

        [sideImage setImage:[UIImage imageNamed:@"oval"]];
    // NSLog(@" write no");
    }

     testButton.myDate=note.mod_time;
    cell.textLabel.text = note.title;

    cell.detailTextLabel.text = dateString;
     [testButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];

     [cell setIndentationLevel:1];
    [cell setIndentationWidth:45];
    [cell.contentView addSubview:sideImage];
    [cell.contentView addSubview:testButton];



    return cell;

}

This is my didselectrowatindexpath:

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

    Notes *note = (Notes *)[[self fetchedResultsControllerCate] objectAtIndexPath:indexPath];

    note.check = [note.check isEqualToString:@"yes"] ? @"no" : @"yes";

    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];
}

now its not working. don't know what i am missing?

david
  • 636
  • 2
  • 12
  • 29

2 Answers2

1

Something like this should work:

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

    Notes *note = (Notes *)[[self fetchedResultsControllerCate] objectAtIndexPath:indexPath];
    note.checked = [note.checked isEqualToString:@"yes"] ? @"no" : @"yes";

    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];
}
Peter Willsey
  • 1,078
  • 6
  • 18
  • its working only for check.when i click my cell row ,its getting check mark. But when i check again its not uncheck – david Sep 04 '15 at 18:57
  • when i add " note.checked = @"no" after " note.checked = @"no"..fully not working – david Sep 04 '15 at 18:59
  • please answer this question ?? @peter – david Sep 04 '15 at 20:00
  • I think what you want is for clicking the cell to toggle the checked state, I updated my answer to do this. – Peter Willsey Sep 04 '15 at 20:02
  • really super.thanks a lot peter. i am well begineer .its help me alot – david Sep 04 '15 at 20:06
  • one more question. with same code above (my post ).i have one add button at bottom of UITableview. i just adjust my table view up and kept my button at centre( of viewcontroller). is there?any possible way to keep my add button to above my UITable view..when we scroll down also my button should stick at centre (Above Of UITableview)..... – david Sep 04 '15 at 20:11
  • You should probably post this as a separate question – Peter Willsey Sep 04 '15 at 20:15
  • http://stackoverflow.com/questions/32406586/how-to-set-make-a-uibutton-at-bottom-of-my-uitableview please see this !! – david Sep 04 '15 at 21:09
  • @peter..i tried all way but cant able to settle button at bottom of tableview – david Sep 04 '15 at 21:28
  • please see my updated post. when i add without detailtextlabel its working. but when i add detailtextlabel it not working.. – david Sep 05 '15 at 09:26
  • please give me some solution for this – david Sep 05 '15 at 19:22
0

you need to make an array for storing selected indexpath. Add or remove the indexpath in/from the array in didSelectRowAtIndexPath method and reload reloadRowsAtIndexPaths accordingly.

  • the below example are working for check mark. while its not unchecking .I am already checking yes or no for selected path (for button check) .i need it for cell selection.that only don't know how to do ?? – david Sep 04 '15 at 19:19