这是我的两个方法,当用户选中或取消选中表格视图时,我需要更新Core Data
中的复选标记。我尝试了很多方法,但无法使用代码更新我的数据。
- (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];
}
}
答案 0 :(得分:0)
好的做一件事去你的核心数据创建一个名为check的其他属性,type应该是string
之后去topmenu吧 - >编辑 - > nsmanageobjectsubclass并在项目中创建它们
之后执行此操作
- (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中获取状态以显示复选框图像,之后我更新第二种方法中的状态,因此当用户选中或选中时选中我们会触发此方法并更新列名称检查coredata。但是,我正在使用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];
}
此更改后,从模拟器或设备中删除您的应用程序并重新编译