2012-02-04 10 views
0

これは、ステータス(チェック済み/未チェック)がCoreDataデータベースに保存されることを願って、各セルのチェックボックスでテーブルビューを実装しようとしています。私は現在の実装でまだ成功していませんでした。もし私がいくつかの助けを得ることができれば、私は非常に感謝します。UITableViewCellのCheckBox付きCoreData

問題のコード:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [pictureListData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 
    // Get the core data object we need to use to populate this table cell 
    Compras *currentCell = [pictureListData objectAtIndex:indexPath.row]; 

    BOOL checked = currentCell.status; 
    UIImage *image = (checked) ? [UIImage imageNamed:@"[email protected]"] : [UIImage imageNamed:@"[email protected]"]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
    button.frame = frame; // match the button's size with the image size 

    [button setBackgroundImage:image forState:UIControlStateNormal]; 

    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet 
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.accessoryView = button; 


     return cell; 
} 

- (void)checkButtonTapped:(id)sender event:(id)event 
{ 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition]; 
    if (indexPath != nil) 
    { 
     [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath]; 
    } 
} 

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

    Compras *currentCell = [pictureListData objectAtIndex:indexPath.row]; 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UIButton *button = (UIButton *)cell.accessoryView; 
    UIImage *image = [UIImage imageNamed:@"[email protected]"]; 

    // patient.check is a string value set to either "Y" or "N" 
    // This allows the check mark to be permanently saved 
    if (currentCell.status) { 
     image = [UIImage imageNamed:@"[email protected]"]; 
     currentCell.status = NO; 
    } 
    else { 
     // image defaults to checked.png 
     image = [UIImage imageNamed:@"[email protected]"]; 
     currentCell.status = YES; 
    } 

    [button setBackgroundImage:image forState:UIControlStateNormal]; 

} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // Get a reference to the table item in our data array 
     Compras *itemToDelete = [self.pictureListData objectAtIndex:indexPath.row]; 

     // Delete the item in Core Data 
     [self.managedObjectContext deleteObject:itemToDelete]; 

     // Remove the item from our array 
     [pictureListData removeObjectAtIndex:indexPath.row]; 

     // Commit the deletion in core data 
     NSError *error; 
     if (![self.managedObjectContext save:&error]) 
      NSLog(@"Failed to delete picture item with error: %@", [error domain]); 

     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 
+0

'currentCell'は' NSManagedObject'と思われます。その 'status'プロパティの型は実際には' BOOL'または 'NSNumber'で' BOOL'値ですか? – Costique

+0

はい、そのBOOL :) – Bruno

答えて

3

あなたはcurrentCellがNSManagedObjectであることを、コメントに書いています。 CoreDataは、オブジェクトのみを扱うように、ブール値を使用すると、BOOLに、オブジェクトのアドレスを書いているとして、あなたのコードは常に、YESになりますのNSNumber

BOOL checked = [currentCell.status boolValue]; 

のインスタンスにでラップされています。ほとんどの場合0になることはありません。

関連する問題