zoukankan      html  css  js  c++  java
  • UITableView的增删改插

    //  AppDelegate.m文件

    #import "AppDelegate.h"

    #import "RootTableViewController.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;

    }

    //  ViewController.h文件

    #import <UIKit/UIKit.h>

    // 1、创建协议

    @protocol PostViewDelegate <NSObject>

    -(void)postValue:(NSString *)userName;

    @end

    @interface ViewController : UIViewController<UITextFieldDelegate>

    @property (strong,nonatomic) NSString *name;

    @property (strong,nonatomic) UITextField *textName;

    @property (strong,nonatomic) id <PostViewDelegate> delegatepp;

    @end

    //  ViewController.m文件

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.view.backgroundColor=[UIColor greenColor];

        self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 44)];

        self.textName.delegate=self;

        self.textName.backgroundColor=[UIColor lightGrayColor];

        

        //文本赋值

        self.textName.text=self.name;

        

        //设置边框样式

        self.textName.borderStyle=1;

        [self.view addSubview:self.textName];   

    }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField

    {

        

        if (self.delegatepp) {

            [self.delegatepp 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

    //  RootTableViewController.h文件

    #import <UIKit/UIKit.h>

    @interface RootTableViewController : UITableViewController

    @property (strong,nonatomic) NSMutableArray *students;

    @end

    //  RootTableViewController.m文件

    #import "RootTableViewController.h"

    #import "ViewController.h"

    @interface RootTableViewController ()<PostViewDelegate>

    {

        //成员变量:存储每次点击的索引值

        NSIndexPath *currentIndexPath;

    }

    @end

    @implementation RootTableViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        //导航栏上的Edit  删除

         self.navigationItem.rightBarButtonItem = self.editButtonItem;

        

        //添加leftBarButtonItem   增加

        self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:4 target:self action:@selector(addItem)];

        

        //数据源

        self.students=[NSMutableArray arrayWithCapacity:0];

        [self.students addObject:@"lisi"];

        [self.students addObject:@"zhangsan"];

        [self.students addObject:@"zhaoliu"];

        [self.students addObject:@"wangwu"];

        

        //指定重用单元格唯一标识

        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myCell"];

    }

    //增加

    -(void)addItem

    {

        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"增加学生信息" message:@"输入姓名" preferredStyle:UIAlertControllerStyleAlert];

        

        UIAlertAction *actionAdd=[UIAlertAction actionWithTitle:@"增加" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            

          UITextField *textName = alertController.textFields[0];

            

            

            [self.students addObject:textName.text];

            //NSLog(@"真正的超作对象");

            [self.tableView reloadData];

        }];

        

        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

            textField.placeholder=@"inpus name";

        }];

        

        [alertController addAction:actionAdd];

        

        [self presentViewController:alertController animated:YES completion:^{

            

        }];

    }

    - (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.students.count;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];

        //主标题显示的内容

        cell.textLabel.text=self.students[indexPath.row]

        ;

        return cell;

    }

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        ViewController *view=[[ViewController alloc]init];

        

        //赋值

        view.name=self.students[indexPath.row];

        //指定代理对象

        view.delegatepp=self;

        //每次点击的索引值

        currentIndexPath=indexPath;

        //下一页

        [self.navigationController pushViewController:view animated:YES];

        

    }

    //代理协议方法的实现

    -(void)postValue:(NSString *)userName

    {

        self.students[currentIndexPath.row]=userName;

        //刷新,重新加载

        [self.tableView reloadData];

         }

    // 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.students removeObject:self.students[indexPath.row]];

            

            //重新加载,相当于[self.tableView reloadData],只是删除哪行,刷新哪行,  withRowAnimation:UITableViewRowAnimationFade 是加一个动画效果

            [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 {

        //找到指定位置的集合元素

        NSString *name=self.students[fromIndexPath.row];

        //删除此集合元素

        [self.students removeObject:name];

        

        //插入此集合元素到指定位置

        [self.students insertObject:name atIndex:toIndexPath.row];

        NSLog(@"%@",self.students);

     }

    // 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;

    }

    @end

  • 相关阅读:
    离散数学知识点总结(8)-图论
    离散数学知识点总结(7)-格
    离散数学知识点总结(6)-计数技术
    离散数学知识点总结(5)函数
    离散数学知识点总结(4)-集合
    离散数学知识点总结(3)-二元关系
    离散数学知识点总结(2)-谓词逻辑
    离散数学知识点总结(1)-命题逻辑
    镜像仓库和Harbor
    视频管理上云平台EasyNVS 2.1版本分享RTSP流和RTMP流端口发生变化是什么原因?
  • 原文地址:https://www.cnblogs.com/Always-LuoHan/p/5293910.html
Copyright © 2011-2022 走看看