2012-02-03 70 views
0

我使用这个代码的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:)]; 
pressRecongnizer.view.tag = indexPath.row; 
//code added 
[pressRecongnizer setValue:[NSNumber numberWithInt:indexPath.row] forKey:@"index"]; 
toDeleteObject = [results objectAtIndex:indexPath.row]; 
pressRecongnizer.minimumPressDuration = 0.5f; 
[cell addGestureRecognizer:pressRecongnizer]; 
[pressRecongnizer release]; 

    // NSIndexPath *indexPathValue = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; 
    // [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone]; 

    // NSLog(@"indexPathValue: %@", indexPathValue); 

cell.textLabel.font = [UIFont systemFontOfSize:10]; 

Favouritesdata *favdata = [results objectAtIndex:indexPath.row]; 

[[cell ignition] setImage:[UIImage imageNamed:@"ignition_flag.png"]]; 
[[cell direction] setImage:[UIImage imageNamed:@"south.png"]]; 

cell.licPlate.text = [favdata licenseplate]; 

NSLog(@"cellvalue for cellforRow: %@", cell.licPlate.text); 

return cell; 
} 

和UILongPressGestureRecognizer代码:

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer{ 

if (recognizer.state != UIGestureRecognizerStateBegan) { 
    return; 
} 

int index = [[recognizer valueForKey:@"index"] intValue]; 

NSLog(@"indexNew: %i", index); 

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] ; 

NSIndexPath *tableSelection = [favouritesTable indexPathForSelectedRow]; 

[favouritesTable selectRowAtIndexPath:tableSelection 
         animated:NO 
       scrollPosition:UITableViewScrollPositionMiddle]; 

NSLog(@"NSIndexPath *tableSelection = %@", tableSelection); 

alert.tag = recognizer.view.tag; 

NSLog(@"alert.tag: %d", alert.tag); 

[alert addButtonWithTitle:@"Remove from Favourites"]; 
[alert addButtonWithTitle:@"Show on Map"]; 

[alert show]; 
} 

在这段代码中我已经申请手势识别器,它有一个对话框,它给用户提供了删除核心数据条目的选项。这里我得到的问题是,我无法得到准确的数据输入值,这将被删除,就像无论单元格/行是否被点击都无关,唯一的核心数据项将被删除,位于索引0 。 我花了很多天来解决这个问题,但没有通过。

请指导我这个问题...提前

感谢

+1

你缺少有关的cellForRowAtIndexPath重要的一点,在它重新使用现有的单元对象进行滚动性能。因此,在上面调用[cell addGestureRecognizer:pressRecognizer]的地方,最终会附加多个识别器。您应该只添加一次。 – peterept 2012-02-03 12:53:37

回答

1

检查方法的名字一次,我没有检查他们

-(void)tableCellPressed:(UILongPressGestureRecognizer*)gust{ 
    UITableViewCell *cell = (id)gust.view; 
    NSIndexPath *ip = [tableView indexPathForCell:cell]; 
}