2012-01-20 6 views
0

私は別の値のための私のテーブルビューにカスタムセルを使用していると、ユーザーが長いために、セルを押したときにダイアログボックスがユーザーに与えて開かれますように、私はUILongPressGestureRecognizerを使用しているコアデータに値を保存します特定のセルの値をコアデータに追加するオプション。テーブルビューセルの値

このための私のコードは次のとおりです。longpressgestureため

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString * cellValue; 
    if (tableView == listTable) 
    { 
     cellValue = [listVehicles objectAtIndex:indexPath.row]; 
    } 
    else // handle search results table view 
    { 
     cellValue = [filteredListItems objectAtIndex:indexPath.row]; 
    } 

    static NSString *CellIdentifier = @"vlCell"; 

    VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     NSLog(@"Cell Created"); 

     NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil]; 

     for (id currentObject in nibObjects) { 
      if ([currentObject isKindOfClass:[VehicleListCell class]]) { 
       cell = (VehicleListCell *)currentObject; 
      } 
     } 

     UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)]; 
     pressRecongnizer.minimumPressDuration = 0.5f; 
     [cell addGestureRecognizer:pressRecongnizer]; 
     [pressRecongnizer release]; 
    } 

    cell.textLabel.font = [UIFont systemFontOfSize:10]; 

    [[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]]; 
    [[cell direction] setImage:[UIImage imageNamed:@"south.png"]]; 

    cell.licPlate.text = cellValue; 

    return cell; 
} 

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer { 

    UITableViewCell *cell = (UITableViewCell *) [recognizer view]; 
    NSString *text = cell.textLabel.text; 

    NSLog(@"cell value: %@", text); 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ; 

    [alert addButtonWithTitle:@"Add to Favourites"]; 
    [alert addButtonWithTitle:@"Take to Map"]; 

    [alert show]; 
} 


-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex { 

    NSString *title = [alert buttonTitleAtIndex:buttonIndex]; 

    if([title isEqualToString:@"Add to Favourites"]) 
    { 
     NSLog(@"Added to favourites."); 
    } 
    else if([title isEqualToString:@"Take to Map"]) 
    { 
     NSLog(@"Go to MapView"); 
    } 
} 

今の問題は、私は任意のセルをクリックした場合、私は唯一のセルの値を取得していますということです。

どのように私はそれを押すことにより、各セルの値を取得し、コアデータに保存することができますか?

EDITED:

私はカスタムセルのXIBファイルとビューコントローラを作成しました:

その.hファイルは通りです:

@interface VehicleListCell : UITableViewCell{ 

IBOutlet UILabel *licPlate; 
IBOutlet UILabel *commDate; 

IBOutlet UIImageView *ignition; 
IBOutlet UIImageView *direction; 

IBOutlet UITableViewCell *cell; 
} 

@property (nonatomic, strong) IBOutlet UILabel *licPlate; 
@property (nonatomic, strong) IBOutlet UILabel *commDate; 
@property (nonatomic, strong) IBOutlet UIImageView *ignition; 
@property (nonatomic, strong) IBOutlet UIImageView *direction; 

@end 

答えて

0

私はあなたのセットアップを行うかどうかはわかりませんVehicleListCellですが、通常のUITableViewとは異なるプロパティを持つ可能性がありますので、変更してください。

UITableViewCell *cell = (UITableViewCell *) [recognizer view]; 

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer 

編集

VehicleListCell* cell = (VehicleListCell *)[recognizer view]; 

このコード

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer { 

    VehicleListCell* cell = (VehicleListCell *)[recognizer view]; 
    NSString *text = cell.licPlate.text; 

    NSLog(@"cell value: %@", text); 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ; 

    [alert addButtonWithTitle:@"Add to Favourites"]; 
    [alert addButtonWithTitle:@"Take to Map"]; 

    [alert show]; 
} 
+0

ありがとうのXスラッシュを試してみてください、私は私の質問を編集した、 –

+0

をチェックしてください私はあなたが持っているもの試してみました提案され、その完了:)しかし、私はこれに関連するもう一つの質問が...私のダイアログボックスは解雇されませんワンクリックで、(いずれかのボタン)を3回クリックすると、ダイアログボックスが消えます。これの背後にある問題は何ですか? –

+0

あなたのNSLogは何度もプリントアウトしますか? (この1のNSLog(「セルの値:%の@」@)、テキスト;)この1つは複数回印刷した場合、あなたのジェスチャー認識は、複数の入力 –

関連する問題