我需要能够在单元格上滑动,而不是显示红色的“删除”按钮,而是显示一个 AlertView,询问用户是否真的要删除。
任何帮助表示赞赏!
您可以将手势识别器添加到您的单元格
UISwipeGestureRecognizer *recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(removeCell:)];
recognizer.direction = UISwipeGestureRecognizerDirectionRight;
[cell addGestureRecognizer:recognizer];
[recognizer release];
然后在 removeCell 方法中
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
UITableViewCell *cell = (UITableViewCell*)[recognizer view];
NSIndexPath* pathOfTheCell = [viewListTable indexPathForCell:cell];
NSInteger rowOfTheCell = [pathOfTheCell row];
NSInteger sectionOftheCell = [pathOfTheCell section];
UIAlertView *confirmationAlert = [[UIAlertView alloc]initWithTitle:@"Confirm" message:@"Are you sure you want to Delete this list?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
[confirmationAlert show];
confirmationAlert.delegate = self;
[confirmationAlert release];
}
在 Alertview 委托中
if (alertView.tag == index)
{
if (buttonIndex == 1)
{
[yourArray removeObjectAtIndex:alertView.tag-1];
[yourTable reloadData];
}
}
添加UISwipeGestureRecognizer
到Cell
UISwipeGestureRecognizer *gesture;
gesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)] autorelease];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:gesture];
gesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)] autorelease];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[cell addGestureRecognizer:gesture];
并在选择器方法提示内确认删除
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
//Use recognizer.direction to check left/right swipe if needed
//Prompt Alert
CGPoint location = [recognizer locationInView:tableView];
NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [tableView cellForRowAtIndexPath:swipedIndexPath];
}
这样做,
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure to commit with its action" delegate:self cancelButtonTitle:CKString(@"NO") otherButtonTitles:CKString(@"YES"),nil];
[Alert show];
Alert.tag=indexPath.row+1;
Alert.delegate=self;
[Alert release];
return UITableViewCellAccessoryNone;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
在 AlertView 委托中
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
int indexPathRow=alertView.tag-1;
if(buttonIndex==1)
{
//// Yes condition
} else {
///// No condition
}
}