美文网首页首页投稿(暂停使用,暂停投稿)程序员iOS开发
iOS开发关于通讯录界面及删除联系人的实现(UI阶段)

iOS开发关于通讯录界面及删除联系人的实现(UI阶段)

作者: 西西哈哈 | 来源:发表于2016-04-21 21:19 被阅读798次

    前面我写的文章中有关于通讯录实现和添加联系人的实现,今天介绍通讯录删除联系人的实现.因为很多初始化的方法在前面添加联系人的文章中都有介绍,本文就不在详细说明了!(删除相比添加而言相对复杂,建议先看添加界面的实现文章).

    在延展中定义数据源数组和tableView

    @property(nonatomic, retain)NSMutableArray *dataSource;
    @property(nonatomic, retain)UITableView *tableView;
    

    在viewDidLoad中添加title和颜色:

    - (void)viewDidLoad {
     [super viewDidLoad];
    
    self.navigationItem.title = @"通讯录";
    self.view.backgroundColor = [UIColor whiteColor];
    }
    

    拖入已有的联系人文件:

    99DF1360-5861-473A-9A4B-2A2FD374911A.png

    初始化tableView:

      self.tableView = [[UITableView alloc] 
     initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 
     self.view.frame.size.height - 64) 
    style:UITableViewStylePlain];
    #设置代理人 (这一步非常关键,千万不要遗漏)
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    [_tableView release];
    

    设置头视图按钮:

    #这些都是比较简单的初始化方法,这里不多介绍
      UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    self.tableView.tableHeaderView = header;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 175, 40);
    button.center = header.center;
    [button setTitle:@"删除" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    button.backgroundColor = [UIColor blueColor];
    [button addTarget:self action:@selector(didClickButton1:) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:button];
    
    [header release];
    

    签订tableView的协议:

        @interface ViewController ()<UITableViewDelegate, 
    UITableViewDataSource>
    

    设置cell的高度:

    //cell的高度
    - (CGFloat)tableView:(UITableView *)tableView 
    heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return 80;
    }
    

    设置可编辑的样式(即删除):

     - (UITableViewCellEditingStyle)tableView:(UITableView   
    *)tableView editingStyleForRowAtIndexPath:(NSIndexPath 
    *)indexPath
    {
     return UITableViewCellEditingStyleDelete;
    }
    

    因为在一个APP中不只一个编辑样式,所以要加判断看是否是删除操作:

    - (void)tableView:(UITableView *)tableView  
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataSource removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    }
    }
    

    tableView必须实现的两个方法:

    cell的行数:
      - (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section
    {
    return self.dataSource.count;
    }
    
    内容:
    - (UITableViewCell *)tableView:(UITableView *)tableView 
      cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    cell.textLabel.text = [[self.dataSource objectAtIndex:indexPath.row] valueForKey:@"name"];
    //判断右侧小图标是否正在被选中:
    if (![self.deleteIndexPathArray containsObject: indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    return cell;
    }
    

    为按钮添加点击方法:

     - (void)didClickButton1:(UIButton *)button
    {
    if (self.deleteIndexPathArray == nil) {
        NSLog(@"数组为空");
        
     }
    else
    {
        //将准备删除的数组放到一个临时数组中:
        NSMutableArray *tempArray = [NSMutableArray array];
        //遍历deleteIndexPathArray
        for (NSIndexPath *indexPath in self.deleteIndexPathArray) {
            NSDictionary *dic = [self.dataSource objectAtIndex:indexPath.row];
            [tempArray addObject:dic];
        }
        [self.dataSource removeObjectsInArray:tempArray];
        [self.deleteIndexPathArray removeAllObjects];
        
    }
    [self.tableView reloadData];
    

    }

    cell的点击方法:

    - (void)tableView:(UITableView *)tableView   
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    //判断数组里是否包含该indexPash
    if ([self.deleteIndexPathArray containsObject:indexPath]) {
        [self.deleteIndexPathArray removeObject:indexPath];
        //取消点击状态:
        cell.accessoryType = UITableViewCellAccessoryNone;
    
    }
    else
    {
        [self.deleteIndexPathArray addObject:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    }
    

    }

    编辑完成 删除点击的cell

        #pragma mark -- 编辑完成:(处理数据,更新视图)
    - (void)tableView:(UITableView *)tableView 
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //判断编辑的样式:
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除数组对应的元素:
        [self.dataSource removeObjectAtIndex:indexPath.row];
        //刷新tableView:
        [tableView reloadData];
        
        }
    }
    

    结合前面几天的添加联系人的实现能更好的理解这部分内容.

    相关文章

      网友评论

        本文标题:iOS开发关于通讯录界面及删除联系人的实现(UI阶段)

        本文链接:https://www.haomeiwen.com/subject/abgtrttx.html