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 。 我花了很多天來解決這個問題,但沒有通過。
請指導我這個問題...提前
感謝
你缺少有關的cellForRowAtIndexPath重要的一點,在它重新使用現有的單元對象進行滾動性能。因此,在上面調用[cell addGestureRecognizer:pressRecognizer]的地方,最終會附加多個識別器。您應該只添加一次。 – peterept 2012-02-03 12:53:37