zoukankan      html  css  js  c++  java
  • ##通讯录阶段重要代码

    #pragma mark -------加载数据的方法---------
    
    //加载数据的方法
    - (void)handleData {
        //获取文件路径
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
        //从文件路径中提取数组
        NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
        //初始化模型数组
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
        for (NSDictionary *dic in array) {
            Student *stu = [[Student alloc] init];
            [stu setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:stu];
            [stu release];
        }
    }
    
    
    #pragma mark -------UITableViewDelegate协议的方法---------
    
    //点击单元格触发的方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        //push传值、推出
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        detailVC.stu = _dataArray[indexPath.row];
        
        [self.navigationController pushViewController:detailVC animated:YES];
        [detailVC release];
    }
    
    
    #pragma mark -------UITableViewDataSource协议的方法---------
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [_dataArray count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *reuseIdentifier = @"reuse";
        //通过重用标识符找cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier] autorelease];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //获取模型
        Student *stu = _dataArray[indexPath.row];
        cell.textLabel.text = stu.name;
        cell.detailTextLabel.text = stu.gender;
        return cell;
    }
    
    
    #pragma mark -------AddViewControllerDelegate协议的方法---------
    
    //实现协议方法
    - (void)sendStudent:(Student *)student{
        [_dataArray insertObject:student atIndex:0];
        //重新加载
        [self.tableView reloadData];
    }
    
    
    #pragma mark -------表视图移动操作---------
    
    //1、移动的第一步也是需要将表视图的编辑状态打开
    //- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    //    //先执行父类中的方法
    //    [super setEditing:editing animated:animated];
    //    //表视图执行此方法
    //    [self.tableView setEditing:editing animated:animated];
    //}
    //2、指定哪些行可以移动,默认都可以移动
    //-------UITableViewDataSource 协议的方法---------
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }
    //3、移动完成后要做什么事,怎么完成移动
    //-------UITableViewDataSource 协议的方法---------
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
        //先记录一下原有位置的模型数据
        Student *student = _dataArray[sourceIndexPath.row];
        [student retain];
        //删除原位置下的模型数据
        [_dataArray removeObjectAtIndex:sourceIndexPath.row];
        //在新位置将记录的模型数据添加到数据数组中
        [_dataArray insertObject:student atIndex:destinationIndexPath.row];
        [student release];
    }
    #pragma mark -------删除、添加数据---------
    
    //1、让将要执行删除、添加操作的表视图处于编辑状态
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated{
        //先执行父类中的方法
        [super setEditing:editing animated:animated];
        //表视图执行此方法
        [self.tableView setEditing:editing animated:animated];
    }
    
    //2、指定表视图中哪些行可以处于编辑状态
    //此方法如果不重写,默认所有的行可以进行编辑
    //-------UITableViewDataSource 协议的方法---------
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row % 2 == 0) {
            return YES;
        }
        return NO;
    }
    
    //3、指定编辑样式,到底是删除还是添加
    //此方法如果不重写,默认是删除样式
    //-------UITableViewDelegate 协议的方法---------
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewCellEditingStyleInsert;
    }
    
    //4、提交,不管是删除还是提交,这个方法才是核心方法,当点击删除、或者添加按钮时,需要做什么事情,怎样才能完成删除或者添加操作,全部都在这里指定
    //点击删除按钮或提交按钮
    //-------UITableViewDataSource 协议的方法---------
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        //1、表视图开始更新
        [tableView beginUpdates];
        //2、判断编辑样式
        if(editingStyle == UITableViewCellEditingStyleDelete) {
            //3、将该位置下的单元格删除
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            //4、删除数据数组中与该单元格绑定的数据
            [_dataArray removeObjectAtIndex:indexPath.row];
        }else if (editingStyle == UITableViewCellEditingStyleInsert) {
            Student *student = _dataArray[indexPath.row];
            //构建一个位置信息
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
            [_dataArray insertObject:student atIndex:index.row];
        }
        //5、表视图结束更新
        [tableView endUpdates];
    }
    
    
    #pragma mark -------实现导航栏上的addBtn的方法---------
    
    - (void)addStu{
        AddViewController *addVC = [[AddViewController alloc] init];
        [self.navigationController pushViewController:addVC animated:YES];
        addVC.delegate = self;
        [addVC release];
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationItem.title = @"通讯录";
        self.view.backgroundColor = [UIColor whiteColor];
        
        UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addStu)];
        self.navigationItem.rightBarButtonItem = addBtn;
        [addBtn release];
        
        //加载数据的方法
        [self handleData];
        //创建表视图
        _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        
        //每一个视图控制器都有一个编辑按钮,因为项目中编辑的应用场景非常多,所以系统预留了一个编辑按钮供我们使用
        self.navigationItem.leftBarButtonItem = self.editButtonItem;
        
        //添加删除
    //    [_tableView setEditing:YES animated:YES];
    }

       

  • 相关阅读:
    ListView点击事件
    ListView优化:
    自定义ListView
    ListView简单使用
    mysql中show processlist过滤和杀死线程
    自定义控件
    yum配置中driver-class-name: com.mysql.jdbc.Driver报错
    CSS+HTML
    maven的配置
    Model、ModelMap、ModelAndView的作用及区别
  • 原文地址:https://www.cnblogs.com/chongyu/p/5209455.html
Copyright © 2011-2022 走看看