2012-01-20 3 views
0

다른 테이블 값에 대한 내 tableview에서 사용자 정의 셀을 사용하고 핵심 데이터에 값을 저장하기 위해 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"); 
    } 
} 

이제 문제입니다 내가 셀 나는 하나의 셀 값을 얻고 클릭합니다.

어떻게 각 셀의 값을 가져 와서 핵심 데이터에 저장할 수 있습니까?

편집 :

의 .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

내가 얼마나 당신에게 설정을 잘 모르겠어요

나는 사용자 정의 셀 XIB 파일로 뷰 컨트롤러를 만들었습니다 귀하의 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 슬래시를 시도에서 14,, 나는 당신이 무엇을 시도 –

+0

을 확인하시기 바랍니다 내 질문을 편집 한 제안 및 그 완료 :) 그러나 나는 이것과 관련된 하나 더 질문이 ... 내 대화 상자가 기각되지 않습니다 한 번의 클릭으로, 나는 (버튼) 3 번 누른 다음 대화 상자가 사라집니다 ..이 뒤에 문제가 될 수있는 무엇입니까 ?? –

+0

NSLog가 여러 번 인쇄됩니까? (이 NSLog (@ "셀 값 : % @", 텍스트);) 제스처 인식기가 두 개 이상의 입력을 감지했기 때문에 여러 번 인쇄하는 경우 일 수 있습니다. –

관련 문제