在cellForRowAtIndexPath:
方法中,我实现了长按手势识别器,
-(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的条目,而不删除从表中选择的条目。
如何解决此问题?
无论如何,您都在向后做,但问题是:
pressRecongnizer.view.tag = indexPath.row; // view is nil at this point
pressRecongnizer.minimumPressDuration = 0.5f;
[cell addGestureRecognizer:pressRecongnizer]; // now we have a view!
[pressRecongnizer release];
在设置视图之前,先在按Recogniser的视图上设置标签。因此,您将一个标记设置为零,最后以零作为标记,这就是您不断删除的行。
在刚创建的单元格中添加一个手势识别器(如果单元格==nil),然后在操作方法中获取单元格的索引路径(UITableView有一个方法indexPathForCell:
),并使用它来确定您选择了哪个元素以及应该删除哪个元素。
让您的uitableviewcell子类实现手势识别器怎么样?这也可以保存对数据对象的引用。这会整洁得多。
假设您的视图控制器持有您的数据模型(这真的是个坏主意),那么您可以使用委托或通知模式进行通信。
这是我脑子里想的东西
@interface VehicleListCell
@property (nonatomic, retain) VehicleClass *vehicle;
@end
@implementation VehicleListCell
@synthesize vehicle;
- (id) initWithStyle:(UITableViewCellStyle) style reuseIdentifier:(NSString*)identifier {
self = [super initWithStyle:style reuseIdentifier:identifier];
if (self) {
//add your gesture recogniser to self using the same code you have
}
return self;
}
- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state != UIGestureRecognizerStateBegan) {
return;
}
[NSNotificationCenter postNotificationName: @"VehicleDeleted" withObject: self userInfo: [NSDictionary dictionaryWithObject: vehicle andKey: @"vehicle"]]
}
@end