2012-01-30 29 views
0

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處的條目而不是從表中選擇的條目。

我該如何解決這個問題?

回答

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的觀點在你設置視圖本身之前。因此,您將標籤設置爲無,最後以零作爲標籤,這是您不斷刪除的行。

第一次創建時(如果cell == nil),爲您的單元格添加一個手勢識別器,然後在您的操作方法中獲取單元格的索引路徑(UITableView有此方法,indexPathForCell:),並使用確定您選擇了哪個元素以及應刪除哪個元素。

+0

我已經在單元格創建方法中實現了手勢識別器,現在我該如何獲得單元格的索引路徑。你能幫助我嗎? – 2012-01-30 12:13:53

+0

indexPath = [self.tableView indexPathForCell:recognizer.view]; – jrturton 2012-01-30 12:30:55

+0

在哪裏添加此調用? – 2012-01-30 12:40:00

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 
+0

我該如何做到這一點?你能幫我用代碼片段 – 2012-01-30 11:17:26

+0

我已經添加了一些東西來向你展示我的意思 – 2012-01-30 11:52:47