• 对简单单元格的增删改


    效果图

    #import <UIKit/UIKit.h>
    
    #import "RootTableViewController.h"
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    
    @end
    
    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[RootTableViewController alloc]initWithStyle:UITableViewStylePlain]];
        return YES;
    }
    
    #import <UIKit/UIKit.h>
    
    
    @interface RootTableViewController : UITableViewController
    
    @property(strong,nonatomic)  NSMutableArray * array;
    
    
    @end
    
    #import "RootTableViewController.h"
    #import "ViewController.h"
    @interface RootTableViewController ()<postValuedelegate>
    {
        //记录选中行的索引值
        NSIndexPath * currentInfrxPath;
    }
    @end
    
    @implementation RootTableViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //添加liftbarabutton
        
        self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem)];
        
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
        
        self.array=[NSMutableArray array];
        
        [self.array addObject:@"zhangsan"];
        
        [self.array addObject:@"lisi"];
        
        [self.array addObject:@"wangwu"];
        
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
        
        
        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;
        
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        
    }
    
    -(void)addItem
    {
        UIAlertController *alertcontroller=[UIAlertController alertControllerWithTitle:@"确定要增加吗" message:@"输入姓名?" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction * ok=[UIAlertAction actionWithTitle:@"增加" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UITextField * textName= alertcontroller.textFields[0];
            
            
            [self.array addObject:textName.text];
            
            [self.tableView reloadData];
            
    //        NSLog(@"真正的操作");
        }];
        
        [alertcontroller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
           
            textField.placeholder=@"添加姓名";
        }];
        
        [alertcontroller addAction:ok];
        
        [self presentViewController:alertcontroller animated:YES completion:nil];
        
    }
    
    -(void)postvalue:(NSString *)username
    {
        //为集合指定索引位置元素赋值
        self.array[currentInfrxPath.row]=username;
        NSLog(@"%@",username);
        //刷新数据
        [self.tableView reloadData];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        return self.array.count;
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
        
        cell.textLabel.text=self.array[indexPath.row];
        
        return cell;
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        currentInfrxPath=indexPath;
        ViewController *vc=[[ViewController alloc]init];
        
        vc.name=self.array[indexPath.row];
        vc.delegate=self;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    
    
    
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            
            [self.array removeObjectAtIndex:currentInfrxPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        } else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }   
    }
    
    
    
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
        //1.找到指定位置集合元素
        NSString * name= self.array[fromIndexPath.row];
        //2.删除集合元素
        [self.array removeObject:name];
        //3插入集合
        [self.array  insertObject:name atIndex:toIndexPath.row];
        
        NSLog(@"%@",self.array);
        
    }
    
    
    
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the item to be re-orderable.
        return YES;
    }
    
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    
    #import <UIKit/UIKit.h>
    
    @protocol postValuedelegate <NSObject>
    
    -(void)postvalue:(NSString *) username;
    
    @end
    
    @interface ViewController : UIViewController<UITextFieldDelegate>
    
    @property(strong,nonatomic) NSString *name;
    
    @property(strong,nonatomic) UITextField * textNmae;
    
    @property(strong,nonatomic) id<postValuedelegate> delegate;
    
    @end
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor=[UIColor greenColor];
        self.textNmae=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 44)];
        
        self.textNmae.delegate=self;
        
        self.textNmae.borderStyle=1;
        
        self.textNmae.text=self.name;
        
        [self.view addSubview:self.textNmae];
    }
    
    
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        
        if (self.delegate) {
            [self.delegate postvalue:textField.text];
        }
        if ([textField isFirstResponder]) {
            [textField resignFirstResponder];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
        
        return YES;
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
  • 相关阅读:
    数据库分库分表之后,你是如何解决事务问题?
    数据库周刊31丨openGauss 正式开源;7月数据库排行榜发布;浙江移动国产数据库AntDB迁移;oracle ADG跨版本搭建;PG解决社保问题;mysqlbinlog解析……
    2020年7月国产数据库排行:华为、腾讯发新品,中兴、阿里结硕果
    47%的MongoDB数据库遭黑客比特币勒索,你中招了吗?中招怎么办?
    数据库周刊30丨数据安全法草案将亮相;2020数据库产业报告;云南电网上线达梦;达梦7误删Redo Log;Oracle存储过程性能瓶颈;易鲸捷实践案例……
    数据库周刊29│2020数据库研究报告;Oracle取消今年技术大会;腾讯云DBbridge发布支持一键迁库;饿了么迁至阿里云;PG数组查询;Oracle被比特币勒索;DM8 安全管理…
    数据库周刊28│开发者最喜爱的数据库是什么?阿里云脱口秀聊程序员转型;MySQL update误操作;PG流复制踩坑;PG异机归档;MySQL架构选型;Oracle技能表;Oracle文件损坏处理……
    怎么查看HBase表的创建时间
    Hadoop/HBase Kerberos认证失败:Clock skew too great
    java线程莫名异常退出时,如何捕获异常信息
  • 原文地址:https://www.cnblogs.com/fume/p/5293015.html
Copyright © 2020-2023  润新知