-1

I have one table view with check mark. when I do check mark one data in my table view my 20,21 data also automatically getting check mark i.e check mark cell reusable.

@interface ViewController ()
{
    NSDateFormatter *formatter;
    BOOL *check;
}

@property (strong) NSMutableArray *notes;
@end
@implementation ViewController
@synthesize tableView;
@synthesize addButton;
@synthesize catefetchedResultsController;

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

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

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

#pragma mark - Table view data source

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

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

    UIButton *testButton;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
       testButton = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
        [testButton setImage:[UIImage imageNamed:@"oval"] forState:UIControlStateNormal];
        [testButton setImage:[UIImage imageNamed:@"tick"] forState:UIControlStateSelected];
        [testButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:testButton];
        [cell setIndentationLevel:1];
        [cell setIndentationWidth:45];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
   // cell.selectionStyle = UITableViewCellSelectionStyleGray;
    // Configure the cell...
        NSManagedObject *note = [self.notes objectAtIndex:indexPath.row];
        NSDate *date = [note valueForKey:@"mod_time"];
        NSString *dateString = [formatter stringFromDate:date];
      cell.textLabel.text = [note valueForKey:@"title"];
        cell.detailTextLabel.text = dateString;
    return cell;
}

- (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
{
        UIButton *btn = (UIButton *)sender;
        if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"oval"]])
        {
            [btn setImage:[UIImage imageNamed:@"tick"] forState:UIControlStateNormal];
        }
        else
        {
            [btn setImage:[UIImage imageNamed:@"oval"] forState:UIControlStateNormal];
                 // other statements
        }
    }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
     //[[_notes objectAtIndex:indexPath.row] checked];

    //[tableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   // _notes.taskArray[indexPath.row][@"CheckStat"] = @YES
}
- (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
        [context deleteObject:[self.notes objectAtIndex:indexPath.row]];

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

        // Remove device from table view
        [self.notes removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
- (IBAction)btnClick:(id)sender {
}
@end
Andrea
  • 11,801
  • 17
  • 65
  • 72
david
  • 636
  • 2
  • 12
  • 29
  • Would you try expanding your description a bit? I wonder if it is rather hard for readers to determine what the difficulty is. – halfer Sep 01 '15 at 08:14
  • Do you need always one checkmark or as many as check mark can remain? – Jamil Sep 01 '15 at 08:17

2 Answers2

0

You can do it by updating the method:

-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
BOOL currentStatus = [[chekMarkArray objectAtIndex:indexPath.row] boolValue];
[chekMarkArray replaceObjectAtIndex:indexPath.row withObject:@(!currentStatus)];
  UIImage *currentImage = nil;
if ([[chekMarkArray objectAtIndex:indexPath.row] intValue]) {
    //cell.accessoryType = UITableViewCellAccessoryCheckmark;
    currentImage = [UIImage imageNamed:@"check.png"];
}else{
    //cell.accessoryType = UITableViewCellAccessoryNone;
    currentImage = [UIImage imageNamed:@"uncheck.png"];
}
//yourCell.yourimageView.image = currentImage;//todo
// [tableView reloadData];
}
Jamil
  • 2,977
  • 1
  • 13
  • 23
  • 'BOOL currentStatus' should be one property of object '_notes'. – Jamil Sep 01 '15 at 08:40
  • .i have check buttom image...how to updata that in your method – david Sep 01 '15 at 08:42
  • i checked you code is working(check<->uncheck shows which cell i tap) but where the problem to find somewhere you upload the code – Jamil Sep 01 '15 at 09:41
  • did you check my code ,its working fine when i check & uncheck,but when me to some 20 data in my table view then if i check first two data,some of my last two data also getting with check mark...@jamail – david Sep 01 '15 at 14:10
0

Every time in cellForRowAtIndexPath: you should get the check state from _notes.taskArray and reset the check mark for the current row.

Jeff Ma
  • 31
  • 2
  • 9