gpt4 book ai didi

ios - 添加一些数据后复选标记重复....当我进行检查时

转载 作者:行者123 更新时间:2023-11-29 01:41:43 26 4
gpt4 key购买 nike

我有一个带有左侧检查按钮的表格 View 。对于前 6 个数据,它运行良好。当我向我的 TableView 添加三个数据之后,当我检查我的第一个数据时,我的 7 数据也得到检查。同样明智的是,当我检查一个数据时,其他一些数据也用复选标记重复..... 我正在使用核心数据....如果有人能回答这个......

  @interface ViewController ()
{

NSDateFormatter *formatter;
}

@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];


UIButton *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;

}


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

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

UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

[tableView deselectRowAtIndexPath:indexPath animated: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

最佳答案

好吧,我看到了你的代码,是的,问题是可重用性,只需更改它,它就会解决你的问题

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

//UIButton *testButton;

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


UIButton *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;




// 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;

}

关于ios - 添加一些数据后复选标记重复....当我进行检查时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32320644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com