我在UILongPressGestureRecognizer上显示一个提醒,我面临的问题是,每次我必须点击三次才能解除警报,而只需单击一下按钮即可解除警报。为什么我必须解雇这个UIAlertView三次?
而且由于这种不正常的行为,入口得到核心数据复制..
我使用的代码是为下:
在的cellForRowAtIndexPath:
-(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;
NSLog(@"cellvalue for cellforRow: %@", cell.licPlate.text);
return cell;
}
在(空白)tableCellPressed:(UILongPressGestureRecognizer *)识别器方法
- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer {
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 addButtonWithTitle:@"Add to Favourites"];
[alert addButtonWithTitle:@"Take to Map"];
[alert show];
}
在警报视图方法中:
-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alert buttonTitleAtIndex:buttonIndex];
NSManagedObjectContext *context = [app managedObjectContext];
Favouritesdata * favourites = [NSEntityDescription insertNewObjectForEntityForName:@"Favouritesdata" inManagedObjectContext:context];
if([title isEqualToString:@"Add to Favourites"])
{
NSLog(@"Added to favourites.");
NSLog(@"cellValueForLongPress: %@", cellValueForLongPress);
if (cellValueForLongPress <= 0) {
NSLog(@"There is no value to save");
}
else {
favourites.licenseplate = cellValueForLongPress;
}
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
else if([title isEqualToString:@"Take to Map"])
{
NSLog(@"Go to MapView");
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Error Occured");}
}
可能是什么问题?
你能帮助我的代码片段,请 –
与例如更新... –
日Thnx @Simon李:)其解决 –