tableView各种模式(删除,移动,排序)

官网tableView的解释:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

先来点理论,最后是一个demo:

当tableView进入编辑模式或者点击编辑的控制,tableView会向delegate发送一系列方法,但是只有代理实现了这些方法,这些方法允许代理重新显示cell的样式和行为。

(注意:当tableView不在编辑模式的时候也可以插入或者删除一系列行!!)

使用setEditing:animated:方法会让tableView进入编辑模式,编辑模式会根据代理实现的方法而由不同,(参见上一段的描述)

比方说实现了 - tableView: moveRowAtIndexPath: toIndexPath:那么编辑模式就还包括了可以移动cell的排序模式,否则没有.

实现了 - tableView:editingStyleForRowAtIndexPath:那么编辑模式根据不同cell,有删除和添加模式‘


滑动的时候要实现tableView:commitEditingStyle:forRowAtIndexPath方法,否则右侧的删除按钮压根不会出现。


- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
可以尝试此方法,去让reload的时候不再有动画。。。







//Demo如下:

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

    NSInteger num;

}

@property (nonatomic,retain) UITableView *tv;

@end


@implementation ViewController


- (void)dealloc

{

    self.tv = nil;

    [super dealloc];

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    num = 2;

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(0, 0, 30, 30);

    [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    UIButton *insertBtn =  [UIButton buttonWithType:UIButtonTypeRoundedRect];

    insertBtn.frame = CGRectMake(50, 0, 30, 30);

    [insertBtn addTarget:self action:@selector(insertBtn:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:insertBtn];

    

    self.tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, 320, 400) style:UITableViewStylePlain];

    self.tv.delegate = self;

    self.tv.dataSource = self;

    [self.view addSubview:self.tv];

}

//按钮对应tableView的编辑模式

-(void)btnPressed:(id)sender

{

    if (self.tv.editing == YES)

    {

        [self.tv setEditing:NO animated:YES];

    }

    else

    {

        [self.tv setEditing:YES animated:YES];

    }

}

//插入cell的按钮

- (void)insertBtn:(id)sender

{

    [self.tv beginUpdates];

    [self.tv insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];

    num ++;

    [self.tv endUpdates];

}

//进入编辑模式,确定编辑提交操作时,执行的代理方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if(editingStyle == UITableViewCellEditingStyleDelete)

    {

        [self.tv beginUpdates];

        [self.tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

        num = 1;

        [self.tv  endUpdates];

    }

    if(editingStyle == UITableViewCellEditingStyleInsert)

    {

        [self.tv beginUpdates];

        

        [self.tv insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

        [self.tv endUpdates];

    }

}


//自定义cell的编辑模式,可以是删除也可以是增加 改变左侧的按钮的样式 删除是'-' 增加是'+'

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 1) {

        return UITableViewCellEditingStyleInsert;

    } else {

        return UITableViewCellEditingStyleDelete;

    }

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return num;

}


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

{

    NSString *cellId = @"fCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if(cell == nil)

    {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId] autorelease];

        cell.contentView.backgroundColor = [UIColor greenColor];

        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        cell.textLabel.text  = @"fanfan";

        cell.indentationLevel = 2;

        cell.indentationWidth = 5; //缩进距离为2*5=10

        cell.imageView.image = [UIImage imageNamed:@"1"];

    }

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d%d%d",indexPath.row,indexPath.row,indexPath.row];

    return cell;

}

//实现这个方法之后,进入编辑模式,那么cell右边之后会出现可以编辑的按钮,长按然后拖动就可以了移动

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    //这里什么都没有实现

    //正常的话,应该在这里实现数据源的排序操作

}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值