ios - 按钮上的 UITableViewCell 单击使用核心数据更新数据库属性值

标签 ios objective-c iphone uitableview core-data

我在我的项目中使用了 CoreData。在我的项目中,一个 TableViewCell 和一个添加按钮。添加按钮单击并移动到另一个 View ,一些文本字段添加到另一个 View 中。然后文本字段文本通过 CoreData 正确保存在数据库中。然后适本地获取值并在 TableViewCell 上重新加载。

我的问题是一个按钮上的 TableViewCell。此按钮用于选中和取消选中单元格。我想按钮是选择和更新数据库上的值。我想要更新相同的实体值。并更新相同的索引值。

假设单击按钮并设置正确的符号图像并将值保存在数据库中相同的表和相同的值中。然后再次单击按钮并设置空白框图像。

现在按钮选择改变图像但不在数据库中保存值。

我只想了解如何更新和删除数据库中的按钮值。在同一个表和同一个索引中。

在此复选框按钮中同步。

我尝试了很多次,但没有正确保存和更新。怎么可能请帮助。谢谢

enter image description here

ViewDidLoad

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

表格 View

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


            }
     }

最佳答案

问题在于您的 buttonPressed 方法。您获取所有 Receipt_Details 对象。尽管您将 newVehicledata_check 声明为 Receipt_Detail 对象,但是 executeFetchRequest 总是返回一个对象数组(即使有只有一个对象),所以它们都是 NSArray

当您在 NSArray 上调用 setValue:forKey: 时,它会在数组的每个元素上调用 setValue:forKey:。所以所有 Receipt_Details 对象都将它们的 isDone 属性设置为“ankur”。

您可能只想更新相关行的 Receipt_Details 对象。您可以通过向获取请求添加谓词来实现此目的,以仅返回相关对象。但是在按钮操作中间执行获取请求是不明智的——获取可能需要一些时间并且用户可能会观察到延迟。更好更容易地使用预先存在的获取对象数组(我假设这就是 self.devices 包含的内容),因为您可以将该行用作该数组的直接索引来定位正确的对象,例如。

Receipt_Detail *newVehicle=[self.devices objectAtIndex:x];

您也可以取消 selectedButtons 数组,因为您可以在每个对象上使用 isDone 属性来指示是否应检查相关行。

关于ios - 按钮上的 UITableViewCell 单击使用核心数据更新数据库属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39674583/

相关文章:

iphone - iOS异常处理问题

ios - 模拟 iOS Firebase Auth 登录方法

ios - Xcode 本地化 Main.strings 文件具有多个具有相同上下文的字符串。将所有内容链接到一个主字符串?

php - AFNetworking 收到服务器未发送的状态代码

objective-c - UITableView 滑动删除 : how to customize button and action?

iphone - Apple 的开发者证书和配置文件

ios - Apple 如何使用 CoreLocation 提供室内定位?

iphone - resignFirstResponder 上的 UITextField 奇怪行为

objective-c - 从点数组中找到最近的点

iphone - 为什么使用 NSFileManager 而不是仅使用 NSData 的 writeToFile :atomically: method when creating a new file?