在cellForRowAtIndexPath:
方法我已經實現手勢識別爲長按刪除從核心數據的數據,問題在通過表視圖iPhone
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == favouritesTable) {
cellValue = [licensePlateArray 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:)];
toDeleteObject = [results objectAtIndex:indexPath.row];
pressRecongnizer.view.tag = indexPath.row;
pressRecongnizer.minimumPressDuration = 0.5f;
[cell addGestureRecognizer:pressRecongnizer];
[pressRecongnizer release];
cell.textLabel.font = [UIFont systemFontOfSize:10];
Favouritesdata *favdata = [results objectAtIndex:indexPath.row];
[[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]];
[[cell direction] setImage:[UIImage imageNamed:@"south.png"]];
cell.licPlate.text = [favdata licenseplate];
NSLog(@"cellvalue for cellforRow: %@", cell.licPlate.text);
return cell;}
而在tableCellPressed
- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state != UIGestureRecognizerStateBegan) {
return;
}
VehicleListCell* cell = (VehicleListCell *)[recognizer view];
cellValueForLongPress = cell.licPlate.text;
NSLog(@"cell value: %@", cellValueForLongPress);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;
alert.tag = recognizer.view.tag;
[alert addButtonWithTitle:@"Remove from Favourites"];
[alert addButtonWithTitle:@"Show on Map"];
[alert show];}
而在alertView:
-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alert buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Remove from Favourites"])
{
NSLog(@"cellValueForLongPress: %@", cellValueForLongPress);
[results removeObjectAtIndex:alert.tag];
[context deleteObject:toDeleteObject];
NSLog(@"alert.tag:::: %d", alert.tag);
}
else if([title isEqualToString:@"Show on Map"])
{
NSLog(@"Go to MapView");
Maps *detailViewFromLabel = [Maps alloc];
[self.view addSubview:detailViewFromLabel.view];
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Error Occured");
}
[favouritesTable reloadData];}
通過此驗證碼輸入核心數據被刪除,但問題是它只刪除索引:0處的條目而不是從表中選擇的條目。
我該如何解決這個問題?
我已經在單元格創建方法中實現了手勢識別器,現在我該如何獲得單元格的索引路徑。你能幫助我嗎? – 2012-01-30 12:13:53
indexPath = [self.tableView indexPathForCell:recognizer.view]; – jrturton 2012-01-30 12:30:55
在哪裏添加此調用? – 2012-01-30 12:40:00