我是iOS的初学者。我正在执行滑动删除选项。我想在删除行之前显示警报视图。我该如何执行此操作。
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",collisionsArray);
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults];
NSString *userId = [userinfo valueForKey:@"user_id"];
if(userId!=nil)
{
NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section];
collisionId = [NSString stringWithFormat:@"%@",[dict valueForKey:@"collisionId"]];
NSLog(@"%@",collisionId);
// removes saved datas from database
BOOL result = [database removeCollisionDetails:collisionId:@"accident_report"];
if(result)
{
[[SHKActivityIndicator currentIndicator]
displayCompleted:NSLocalizedString(@"val_sucess_vehicle", nil)];
[self.navigationController popViewControllerAnimated:YES];
}
else
{
[[SHKActivityIndicator currentIndicator]
displayCompleted:NSLocalizedString(@"val_error", nil)];
}
}
}
[self.tableView reloadData];
}
答案 0 :(得分:2)
为此,您只需在以下位置显示警报视图:
if (editingStyle == UITableViewCellEditingStyleDelete){
// Show your alert view
// Set its delegate to self
}
现在你必须做类似的事情:
#pragma mark ---- Delegate for alertview ----
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults];
NSString *userId = [userinfo valueForKey:@"user_id"];
if(userId!=nil)
{
NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section];
collisionId = [NSString stringWithFormat:@"%@",[dict valueForKey:@"collisionId"]];
NSLog(@"%@",collisionId);
// removes saved datas from database
BOOL result = [database removeCollisionDetails:collisionId:@"accident_report"];
if(result)
{
[[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(@"val_sucess_vehicle", nil)];
[self.navigationController popViewControllerAnimated:YES];
}
else
{
[[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(@"val_error", nil)];
}
}
}
答案 1 :(得分:1)
-(void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warring" message:@"Are You Sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes ", nil];
[alert show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
}else if (buttonIndex == 1){
NSIndexPath *indexPath=[_UserTableView indexPathForSelectedRow];
[showUData removeObjectAtIndex:indexPath.row]; //showUData NSMutableArray
[_UserTableView reloadData];
[storeData setObject:showUData forKey:@"SendData"]; //storeData NSUserDefault
[storeData synchronize];
}
}
答案 2 :(得分:0)
使用UIAlertView
并仅在用户确认时删除。将您的代码更新为:
if (editingStyle == UITableViewCellEditingStyleDelete)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hello World!"
message:@"Are you sure ?"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
实施UIAlertViewDelegate
并将原始删除代码移动到委托方法,您可以在其中检测已点击的按钮。