通过表格视图iPhone从核心数据中删除数据的问题

时间:2012-01-30 10:53:27

标签: iphone objective-c ios cocoa-touch core-data

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而不是从表中选择的条目。

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

如何让你的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

答案 1 :(得分: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];

在设置视图本身之前,您可以在pressRecogniser的视图上设置标记。因此,您将标记设置为空,最后以零作为标记,这是您要删除的行。

首次创建手势识别器(如果单元格== nil),然后在操作方法中获取单元格的索引路径(UITableView有一个方法,indexPathForCell:) ,并使用它来确定您选择了哪个元素以及应删除哪个元素。