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