2010-12-13 6 views
1

CoreDataBooksに基づいたアプリケーションがあります。 iOSサンプルコードに記載されているように、セルアクセサリボタンで2つの画像を切り替える必要がありますが、ロードブロッキングを実行しています。 accessoryButtonTappedForRowWithIndexPath:メソッドに記載されているUITableViewCellを取得する方法を理解できません。サンプルではアイテム内のオブジェクトがキー値として格納されていましたが、私は患者とコアデータでこれを行う方法を理解できません。すべてのCore Dataを使用してAccessoryサンプルでUITableViewCellを取得する方法

まず、ここでは関係アクセサリーのサンプルからのコードです:私は「Y」または「N」のプロパティを「チェック」を用いて正確に私の画像を設定することができる午前

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

そのI

- (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]; 
} 
下に述べたように、CoreDataで保存することができます

私は助けていただきありがとうございます。

補正: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(); 
    } 
} 
+0

ありがとうございました。ソースコードを受け取ることは可能ですか?いずれの答えも高く評価されます。 – Foo

答えて

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]; 
} 
+0

ありがとう!これは私が必要としていた作品でした。これをコアデータに保存するなど、最終的な解決策を投稿するように私の質問を修正しました。 – jon

+1

@jon:私が助けてくれてうれしい! :) – BoltClock

関連する問題