выберите строку ячейки вместо кнопки проверки, чтобы установить / снять флажок в UITableView

Это обновленная запись.

у меня есть один Viewcontroller с табличным представлением, кнопка проверки (слева). когда я нажимаю кнопку проверки в табличном представлении, она хорошо работает для проверки/снятия отметки. Она работает нормально.

Необходимо:

Несмотря на нажатие кнопки проверки в моем табличном представлении. мне нужно щелкнуть мою строку ячейки, в то время конкретная выбранная кнопка проверки строки ячейки должна установить / снять флажок. как к этому я пробовал все пути. но решение не получено. Вот мой файл 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"]) {


            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

Это мой 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;

}

Это мой путь к didselectrowatindex:

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

теперь он не работает. не знаю, что мне не хватает?


person david    schedule 04.09.2015    source источник


Ответы (2)


Что-то вроде этого должно работать:

- (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];
}
person Peter Willsey    schedule 04.09.2015
comment
он работает только для проверки. Когда я нажимаю на строку своей ячейки, она получает галочку. Но когда я снова проверяю, он не снимается - person david; 04.09.2015
comment
когда я добавляю note.checked = @no после note.checked = @no..полностью не работает - person david; 04.09.2015
comment
пожалуйста ответьте на этот вопрос?? @Питер - person david; 04.09.2015
comment
Я думаю, что вы хотите, чтобы щелкнуть ячейку, чтобы переключить проверенное состояние, я обновил свой ответ, чтобы сделать это. - person Peter Willsey; 04.09.2015
comment
действительно супер. Большое спасибо, Питер. я хорошо начинаю. это мне очень помогает - person david; 04.09.2015
comment
еще один вопрос. с тем же кодом выше (мой пост). У меня есть одна кнопка добавления внизу UITableview. я просто настроил вид таблицы и держал кнопку в центре (контроллера просмотра). есть ли какой-либо возможный способ сохранить мою кнопку добавления над моим представлением UITable.. когда мы прокручиваем вниз, моя кнопка также должна оставаться в центре (над UITableview) ..... - person david; 04.09.2015
comment
Вероятно, вам следует опубликовать это как отдельный вопрос - person Peter Willsey; 04.09.2015
comment
stackoverflow.com/questions/32406586/ посмотрите это!! - person david; 05.09.2015
comment
@peter.. я пытался изо всех сил, но не могу установить кнопку внизу таблицы - person david; 05.09.2015
comment
пожалуйста, смотрите мой обновленный пост. когда я добавляю без detailtextlabel, он работает. но когда я добавляю detailtextlabel, он не работает.. - person david; 05.09.2015
comment
пожалуйста, дайте мне какое-нибудь решение для этого - person david; 05.09.2015

вам нужно создать массив для хранения выбранного indexpath. Добавьте или удалите indexpath в/из массива в методе didSelectRowAtIndexPath и соответственно перезагрузите reloadRowsAtIndexPaths.

person Archit Dhupar    schedule 04.09.2015
comment
приведенный ниже пример работает для галочки. пока он не снимается. Я уже проверяю да или нет для выбранного пути (для проверки кнопки). Мне это нужно для выбора ячейки. Вот только не знаю, как это сделать ?? - person david; 04.09.2015