ios初学者之Tableview的增删移

在Android上实现列表的新增和删除很容易,但是效果单调的很,而且对于android上的listview+adapter写起来是如此的复杂,好在google出了recyclerview来替换listview,才让android的列表富有灵活的扩展性,当相对于ios的tableview来说,我只能说,这个tableview控件太强大了。于是琢磨了半天,写下了tableview的增删编辑状态效果。效果如下

附上controller代码:

//
//  ViewController.m
//  tableview增删操作
//
//  Created by taofuxn on 16/11/2.
//  Copyright © 2016年 taofuxn. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

- (IBAction)edtTableview:(id)sender;

@property (weak, nonatomic) IBOutlet UIButton *add;
@property (weak, nonatomic) IBOutlet UIButton *del;

@property (weak, nonatomic) IBOutlet UITableView *table;
@end

@implementation ViewController


NSMutableArray *list;

NSInteger action;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //初始化数据
    list = [[NSMutableArray alloc]initWithObjects:@"德玛西亚", @"卡特琳娜",@"疾风剑豪",@"泷泽萝拉",@"奥巴马",@"鬼魅妖姬",nil];
    
    action = 0 ;
    
    
    self.table.delegate = self;
    self.table.dataSource = self;
   
}


//返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [list count];
}

//创建了单元格
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *tag = @"taofuxn";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tag];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tag];
    }
    //赋值
    cell.textLabel.text = [list objectAtIndex:indexPath.row];
    return cell;
}

//返回单元格的编辑状态
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return action == 0 ? UITableViewCellEditingStyleDelete:UITableViewCellEditingStyleInsert;
}

//返回行删除时的文本
-(NSString*) tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"确认删除";
}

//决定哪一行可以编辑的状态
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 1) {
        return NO;
    }
    
    return YES;
}

//当移动完成时激发该方法
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //当前下标
    NSInteger sourceRow = sourceIndexPath.row;
    //指定下标
    NSInteger toRow = destinationIndexPath.row;
    
    //先保存要移除的数据
    id tagObj = [list objectAtIndex:sourceRow];
    //先移除
    [list removeObjectAtIndex:sourceRow];
    //后插入
    [list insertObject:tagObj atIndex:toRow];
    
    
    
}

//    删除和插入完成事执行该方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath .row;
//    如果状态是删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete){
        //从数据上删除该数据
        [list removeObjectAtIndex:row];
        //从界面上删除指定的行
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
//    如果是插入操作
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        //在当前行的下一行插入一相同的数据
        [list insertObject:[list objectAtIndex:row] atIndex:row+1];
        //从界面上删除指定的行
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

}



- (IBAction)edtTableview:(id)sender {
    UIButton *btn = sender;
    //tag->0,1
    action = btn.tag;
    
    //使用动画来切换表格的编辑状态也回调上面那个状态的方法 editingStyleForRowAtIndexPath
    [self.table setEditing:!self.table.isEditing animated:YES];
    
    //根据表格编辑状态改变按钮状态
    if (self.table.isEditing) {
       self.add.titleLabel.text = @"完成";
        self.del.titleLabel.text = @"完成";
    }else{
        self.add.titleLabel.text = @"添加";
        self.del.titleLabel.text = @"删除";
    } 
}
@end

就这么一个文件就实现了列表的删除插入和移动,不过这似乎用了系统的删除方式,如果能仿照qq的侧滑删除就更好了。研究下

目前对于这个tableview还有几个问题:

1、cell单元格是怎么控制高度的,怎么样做到自适应的高度

2、上拉加载下拉刷新

3、侧滑删除

4、类似android的toast,在ios中该怎么实现

 

posted @ 2016-11-02 12:31  掏富小牛  阅读(226)  评论(0编辑  收藏  举报