gpt4 book ai didi

iphone - 如何通过 TableView 获取索引值来删除核心数据条目

转载 作者:行者123 更新时间:2023-11-29 04:49:19 24 4
gpt4 key购买 nike

我将此代码用于 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 处的唯一核心数据条目将被删除。我花了很多天来解决这个问题,但没有解决。

请指导我解决这个问题...

提前致谢

最佳答案

检查方法名称一次,我没有检查

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

关于iphone - 如何通过 TableView 获取索引值来删除核心数据条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9128584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com