【问题标题】:How to obtain UITableViewCell in Accessory sample using Core Data如何使用 Core Data 在 Accessory 示例中获取 UITableViewCell
【发布时间】:2011-05-24 10:57:27
【问题描述】:

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

首先,这里是附件示例中的相关代码:

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

我可以使用可以存储在 CoreData 中的“Y”或“N”的“检查”属性来正确设置图像,如下所述:

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

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

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

感谢您的帮助。

修正:多亏了 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();
    }
}

【问题讨论】:

  • 感谢您的清晰帖子。是否可以接收源代码?任何答案表示赞赏。

标签: iphone objective-c xcode core-data uitableview


【解决方案1】:

使用表格视图的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];
}

【讨论】:

  • 谢谢!这是我需要的作品。修改了我的问题以发布最终解决方案,包括将其保存到核心数据。
  • @jon:很高兴我能帮上忙! :)
猜你喜欢
  • 2012-09-20
  • 2014-05-22
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-16
相关资源
最近更新 更多