use*_*396 8 objective-c uitableview uialertview swift uialertcontroller
我想删除一个表视图单元格,但在该操作发生之前,我想给用户一个警报视图.我懂了:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Are you sure?"
delegate:self
cancelButtonTitle:@"NO"
otherButtonTitles:@"YES", nil];
[alert show];
[self.array removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Nee"])
{
NSLog(@"Nothing to do here");
}
else if([title isEqualToString:@"Ja"])
{
NSLog(@"Delete the cell");
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在当我在单元格上向右滑动并且删除按钮出现时,我没有AlertView.当我按下删除按钮时,我只获得AlertView.当我按下删除按钮时,将显示该消息但该单元格已被删除.
如何使这项工作?所以当我滑动时有一个AlertView.
Her*_*ker 17
关于序列,一切都很好.commitEditingStyle只有在按下删除按钮时才会调用.关键是你实际上在响应警报之前删除了对象.把它改成这个:
在此之前将其添加到.m文件中@implementation:
@interface PutYourViewControllerClassNameHere
@property (strong, nonatomic) NSIndexPath *indexPathToBeDeleted;
@end
Run Code Online (Sandbox Code Playgroud)
然后:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
self.indexPathToBeDeleted = indexPath;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Are you sure?"
delegate:self
cancelButtonTitle:@"NO"
otherButtonTitles:@"YES", nil];
[alert show];
// do not delete it here. So far the alter has not even been shown yet. It will not been shown to the user before this current method is finished.
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// This method is invoked in response to the user's action. The altert view is about to disappear (or has been disappeard already - I am not sure)
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"NO"])
{
NSLog(@"Nothing to do here");
}
else if([title isEqualToString:@"YES"])
{
NSLog(@"Delete the cell");
[self.array removeObjectAtIndex:[self.indexPathToBeDeleted row]];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPathToBeDeleted] withRowAnimation:UITableViewRowAnimationFade];
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这应该编译,尽管可能有轻微的语法错误.一般假设:您只处理一个部分.至少只有删除内的一个部分是可能的.
iOS 8 推出UIAlertController。这允许您在完成块而不是委托方法中编写删除和取消代码(按照-clickedButtonAtIndex旧的UIAlertView)。
斯威夫特 3
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let alertController = UIAlertController(title: "Warning", message: "Are you sure?", preferredStyle: .alert)
let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (action) in
self.tableView.deleteRows(at: [indexPath], with: .fade)
})
alertController.addAction(deleteAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
目标-C
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Are you sure?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}];
[alertController addAction:deleteAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Don't do anything");
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6750 次 |
| 最近记录: |