2015-09-04 45 views
0

這是更新後選擇單元格行而不是檢查按鈕在UITableView中進行檢查/取消選中標記

我有一個ViewController與tableview,檢查按鈕(左側)。當我點擊我的表格檢查按鈕查看它的工作很好檢查/ uncheck.its工作正常。

需要:點擊我的表視圖檢查按鈕的

Inspite。我需要點擊我的單元格行,那時特定的單元格行檢查按鈕應該選中/取消選中。如何,我已經嘗試了所有的方式。但沒有解決方案得到。這裏是我的viewcontroller.m文件:

在此先感謝!

@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"]) { 


      [email protected]"yes"; 
      NSLog(@" write yes"); 
       btn.selected=NO; 

      } 
      else 
      { 
       [email protected]"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 

這是我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; 

} 

這是我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]; 
} 

現在它不工作。不知道我錯過了什麼?

回答

1

像這樣的東西應該工作:

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

其工作只爲check.when我點擊我的細胞行,其獲取複選標記。但是,當我再次檢查其不取消 – david

+0

當我在「note.checked = @」no「後添加」note.checked = @「no」..完全不工作 – david

+0

請回答這個問題?? @peter – david

0

你需要做一個數組存儲選擇indexpath。在didSelectRowAtIndexPath方法中的數組中添加或刪除indexpath,並相應地重新加載reloadRowsAtIndexPaths。

+0

下面的例子正在爲複選標記的一些解決方案。雖然它不是取消勾選。我今年已經是檢查或不選定路徑(按鈕查看)。我需要它的細胞selection.that只是不知道該怎麼辦? – david