0

I have used custom cell in my tableview for different values and to save value into core data I have used UILongPressGestureRecognizer, so that when user presses the cell for long then a dialogue box will be opened giving user the option to add the value of that specific cell into core data.

My code for this is :

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

for longpressgesture:

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

Now the issue is that if I click on any cell I am getting the only one cell's value.

How can I get the value of each cell by pressing on it and then save it to core data ?

EDITED:

I have created a view controller with xib file for custom cell:

its .h file is as:

@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
Omer Waqas Khan
  • 2,423
  • 4
  • 33
  • 62

1 Answers1

0

I'm not sure how do you setup your VehicleListCell, but it might have different property than normal UITableView, so change your

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

in

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer

to

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

edit

try this code

- (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];
}
X Slash
  • 4,133
  • 4
  • 27
  • 35
  • I have tried what you have suggested, and its done :) But I have one more question related to this ... my dialog box does not gets dismissed on single click, I have to click (either button) 3 times then the dialog box disappears.. what can be the issue behind this ?? – Omer Waqas Khan Jan 20 '12 at 13:56
  • does your NSLog prints out multiple times ? (This one NSLog(@"cell value: %@", text);) if this one prints multiple times, it might be because your gesture recognizer detects more than one input – X Slash Jan 20 '12 at 14:01
  • but every time when I long press, dialog box needs to be clicked 3 times to disappear – Omer Waqas Khan Jan 20 '12 at 14:03
  • read this link, http://stackoverflow.com/questions/3319591/uilongpressgesturerecognizer-gets-called-twice-when-pressing-down, this should help! cheers – X Slash Jan 20 '12 at 14:08