如何使用Core Data在Accessory示例中获取UITableViewCell

jon*_*jon 1 iphone xcode core-data objective-c uitableview

我有一个基于CoreDataBooks的应用程序.我想在iOS示例代码中提到的单元附件按钮中切换两个图像,但我遇到了障碍.我无法弄清楚如何获取在accessoryButtonTappedForRowWithIndexPath:方法中记录的UITableViewCell.该示例将对象存储在项目中作为键值,但我无法弄清楚如何使用我的患者和核心数据执行此操作.

首先,这是Accessory示例中的相关代码:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }

    NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"text"];

    [item setObject:cell forKey:@"cell"];

    BOOL checked = [[item objectForKey:@"checked"] boolValue];
    UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];

    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
{   
    NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];

    BOOL checked = [[item objectForKey:@"checked"] boolValue];

    [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];

    UITableViewCell *cell = [item objectForKey:@"cell"];
    UIButton *button = (UIButton *)cell.accessoryView;

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)

我可以通过使用"Y"或"N"的"check"属性来正确设置我的图像,我可以将其存储在CoreData中,如下所示:

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

    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];

    cell.textLabel.text = patient.name;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    UIImage *image = [UIImage imageNamed:@"unchecked.png"];

    if ([patient.check isEqualToString: @"Y"] == YES)
        image = [UIImage imageNamed:@"checked.png"];

        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;
}
Run Code Online (Sandbox Code Playgroud)

但是,我在使用此方法时遇到问题,如代码中所述:

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

    Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];

    // HOW CAN I OBTAIN THE UITABLEVIEWCELL?    
    UITableViewCell *cell = [item objectForKey:@"cell"];
    UIButton *button = (UIButton *)cell.accessoryView;

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)

我感谢任何帮助.

修正:感谢BoltClock,这一切都在一起.发布最终代码以防其他人想要使用此方法.

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

    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];

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

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

    [button setBackgroundImage:image forState:UIControlStateNormal];

    // Save patient.check value
    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}
Run Code Online (Sandbox Code Playgroud)

Bol*_*ock 6

使用表视图的cellForRowAtIndexPath:方法并将索引路径作为参数传递.

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

    Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *button = (UIButton *)cell.accessoryView;

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)