Ios 如何更新我的核心数据字段。我什么时候做了一些改变?
这是我的两种方法,当用户选中或取消选中表视图时,我需要更新Ios 如何更新我的核心数据字段。我什么时候做了一些改变?,ios,Ios,这是我的两种方法,当用户选中或取消选中表视图时,我需要更新核心数据中的复选标记。我尝试了很多方法,但无法使用代码更新数据 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cell"; UITableViewCell *cell = [tableVie
核心数据中的复选标记。我尝试了很多方法,但无法使用代码更新数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
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)buttonTouched:(id)sender
{
UIButton *btn = (UIButton *)sender;
if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"oval"]]) {
//[tableView deselectRowAtIndexPath:indexPath animated:YES];
[btn setImage:[UIImage imageNamed:@"tick"] forState:UIControlStateNormal];
} else {
[btn setImage:[UIImage imageNamed:@"oval"] forState:UIControlStateNormal];
}
}
好的,做一件事,转到核心数据,创建另一个名为check的属性,类型应该是string
然后转到顶部菜单栏->编辑器->nsmanageobjectsubclass并在project中创建它们
在那之后做这个
- (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"]){
[sideImage setImage:[UIImage imageNamed:@"tick"]];
}else{
// [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;
}
在上面的代码中,我只是尝试从cordite获取状态以显示复选框图像,然后我在第二个方法中更新状态,因此当用户在check或uncheck上单击时,我们触发此方法并更新coredata中的列名check。但是,是的,我使用的是fetchviewcontroler及其委托,它简单地直接获取任何更新,所以在这方面我们不需要做太多的工作
-(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];
}
更改后,从模拟器或设备中删除应用程序并重新编译