ios 关于基础弹窗添加数据和编辑删除的坑

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{
//练习一下持久化
    NSUserDefaults *user;
}
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *arr;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    user=[NSUserDefaults standardUserDefaults];
    self.title = @"小组名单";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleDone target:self action:@selector(left)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"+" style:UIBarButtonItemStyleDone target:self action:@selector(right)];
    
    self.arr = [NSMutableArray array];
    self.tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tab.delegate=self;
    self.tab.dataSource=self;
    [self.view addSubview:self.tab];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[self.tab dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.detailTextLabel.text=@"1";
    cell.textLabel.text=self.arr[indexPath.row];
    return cell;
}
-(void)right{
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"听说你要添加数据?" message:nil preferredStyle:UIAlertControllerStyleAlert];
    //添加一个文本框
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder=@"请输入添加的数据";
        //设置文本框代理
        textField.delegate = self;
    }];
    //创建一个文本框按钮
    UIAlertAction *action1=[UIAlertAction actionWithTitle:@"确定" style:UIAlertControllerStyleAlert handler:^(UIAlertAction * _Nonnull action) {
    //将数组值传给H文件中
        [user setObject:self.arr forKey:@"H"];
        //同步
        [user synchronize];
        NSLog(@"-----%@",[user objectForKey:@"H" ]);
        //确定后刷新表格
        [self.tab reloadData];
        
        
    }];
    
    UIAlertAction *action11=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [alert addAction:action1];
    [alert addAction:action11];
    
    [self.navigationController presentViewController:alert animated:NO completion:nil];
}
//视图打开的时候进行查找H文件中的数据进行展示
-(void)viewWillAppear:(BOOL)animated{
//将H文件中的内容给新的数组
    NSArray *arr1=[user objectForKey:@"H"];
    //添加到数据源
    [self.arr addObjectsFromArray:arr1];
     NSLog(@"视图钢价在:%@",arr1);
     //刷新表格
    [_tbv reloadData];
}
//调用文本框添加数据到数组
-(void)textFieldDidEndEditing:(UITextField *)textField{
    [self.arr addObject:textField.text];
}
//表格左滑删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //删除数据
    [self.arr removeObjectAtIndex:indexPath.row];
    //刷新表格
    [self.tab reloadData];
}
//点击左边编译
-(void)left{
    if(self.tab.editing==YES){
        [self.tab setEditing:NO animated:YES];
    }else{
        [self.tab setEditing:YES animated:YES];
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,您的问题不太清晰,请问您是想实现iOS应用程序中首次打开时弹出网络连接提示框吗?如果是的话,可以参考以下步骤: 1. 在您的应用程序中添加一个网络检测功能,判断当前设备是否联网。可以使用Reachability库实现。 2. 在应用程序的AppDelegate文件中添加以下代码,在应用程序启动时检测网络状态,并根据需要显示提示框: ```swift func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // 检测网络状态 let reachability = Reachability.forInternetConnection() let status = reachability?.currentReachabilityStatus() // 如果网络不可用,弹出提示框 if status == NotReachable { let alertController = UIAlertController(title: "网络连接错误", message: "请检查您的网络连接", preferredStyle: .alert) let okAction = UIAlertAction(title: "确定", style: .default, handler: nil) alertController.addAction(okAction) self.window?.rootViewController?.present(alertController, animated: true, completion: nil) } return true } ``` 3. 为了避免每次启动应用程序都弹出提示框,您可以使用UserDefaults来保存用户是否已经看过提示框的状态。只有在用户第一次启动应用程序时才弹出提示框。 ```swift func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // 检测网络状态 let reachability = Reachability.forInternetConnection() let status = reachability?.currentReachabilityStatus() // 如果网络不可用,弹出提示框 if status == NotReachable { let userDefaults = UserDefaults.standard let hasShownAlert = userDefaults.bool(forKey: "HasShownNetworkAlert") if !hasShownAlert { let alertController = UIAlertController(title: "网络连接错误", message: "请检查您的网络连接", preferredStyle: .alert) let okAction = UIAlertAction(title: "确定", style: .default, handler: nil) alertController.addAction(okAction) self.window?.rootViewController?.present(alertController, animated: true, completion: nil) userDefaults.set(true, forKey: "HasShownNetworkAlert") userDefaults.synchronize() } } return true } ``` 希望这些步骤能够对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值