我在我的项目中使用CoreData。在我的项目中有一个TableViewCell和一个添加按钮。添加按钮单击并移动到另一个视图,并在一个文本字段中添加另一个视图。然后文本字段文本由CoreData在数据库中保存。然后在TableViewCell上正确获取值并重新加载。
我的问题是TableViewCell在一个按钮上。此按钮用于检查和取消选中单元格。我想按钮是选择并更新数据库上的值。我想要一个相同的实体值进行更新。并且更新相同的索引值。
假设单击按钮并设置正确的符号图像并将值保存在数据库相同的表和相同的值中。然后再次单击按钮并设置空白框图像。
现在按钮选择更改图像,但不保存数据库中的值。
我只想更新和删除数据库中的按钮值。在同一个表和相同的索引中。
此复选框按钮中的syns。
我尝试了很多次,但没有正确保存和更新。怎么可能请帮忙。谢谢
- (void)viewDidLoad {
[super viewDidLoad];
selectedButton = [[NSMutableArray alloc]init];//i've already defined the array at the .h file
for (int i = 0; i<90000; i++) //yourTableSize = how many rows u got
{
[selectedButton addObject:@"NO"];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"tableview cell");
All_Receipt_Cell *cell = [_table_view dequeueReusableCellWithIdentifier:@"htrcell"];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[cell.date_lbl setText:[device valueForKey:@"date"]];
[cell.company_lbl setText:[device valueForKey:@"company"]];
[cell.total_lbl setText:[device valueForKey:@"total"]];
cell.currency_lbl.text = @"$";
UIImage *img = [UIImage imageNamed:@"images.jpeg"];
UIButton*toggleButton= cell.toggleButton;
[toggleButton setImage:img forState:UIControlStateNormal];
img = [UIImage imageNamed:@"images.png"];
[toggleButton setImage:img forState:UIControlStateSelected];
[toggleButton setTag:indexPath.row+100];//set the tag whichever way you wanted it, i set it this way so that the button will have tags that is corresponding with the table's indexpath.row
[toggleButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:toggleButton];
//and now we set the button's selected state, everytime the table reuse/redraw the cell the button will set it's selected state according to the array
if([[selectedButton objectAtIndex:indexPath.row]isEqualToString:@"NO"])
{
[toggleButton setSelected:NO];
}
else
{
[toggleButton setSelected:YES];
}
return cell;
}
-(void)buttonPressed:(UIButton*)sender
{
int x = sender.tag - 100; //get the table's row
if([sender isSelected] ) //if the button is selected, deselect it, and then replace the "YES" in the array with "NO"
{
[selectedButton replaceObjectAtIndex:x withObject:@"NO"];
[sender setSelected:NO];
NSLog(@"unselected");
NSFetchRequest *fetchRequest=[NSFetchRequest fetchRequestWithEntityName:@"Receipt_Details"];
Receipt_Detail *newVehicle=[self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
[newVehicle setValue:@"ankur" forKey:@"isDone"];
[self.managedObjectContext save:nil];
}
else if (![sender isSelected]) //if the button is unselected, select it, and then replace the "NO" in the array with "YES"
{
[selectedButton replaceObjectAtIndex:x withObject:@"YES"];
[sender setSelected:YES];
NSLog(@"selected %@",sender.titleLabel.text);
NSLog(@"tag number is = %ld",(long)[sender tag]);
NSFetchRequest *fetchRequest=[NSFetchRequest fetchRequestWithEntityName:@"Receipt_Details"];
Receipt_Detail *data_check=[self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
[data_check setValue:sender.titleLabel.text forKey:@"isDone"];
[self.managedObjectContext save:nil];
}
}
答案 0 :(得分:1)
问题在于您的buttonPressed
方法。您获取所有Receipt_Details
个对象。虽然您将newVehicle
和data_check
声明为Receipt_Detail
个对象,但executeFetchRequest
始终返回一个对象数组(即使只有一个对象),因此它们都是{{1} }}第
当您在NSArray
上致电setValue:forKey:
时,会在阵列的每个元素上调用NSArray
。因此,所有setValue:forKey:
个对象的Receipt_Details
属性都设置为&#34; ankur&#34;。
您可能只希望更新相关行的isDone
对象。您可以通过向获取请求添加谓词来实现此目的,以仅返回相关对象。但是在按钮操作过程中执行获取请求是不明智的 - 获取可能需要一些时间,用户可能会观察到延迟。更好更容易地使用预先存在的已获取对象数组(我假设Receipt_Details
包含的内容),因为您可以使用该行作为该数组的直接索引来定位正确的对象,例如
self.devices
您也可以取消使用Receipt_Detail *newVehicle=[self.devices objectAtIndex:x];
数组,因为您可以在每个对象上使用selectedButtons
属性来指示是否应该检查相关行。