UITableView 中动态弹出Cell实现,InLine Cell的实现,模仿iOS 原声应用 Calendar 实现弹出Cell

这个案例最先是在 iOS 官方的应用 — Calendar 中有一个In_line Date Picker的效果,其实就是 EKEventKit 中的 EKEventEditViewController 也包含了这个 In_line 的效果。

这里写一下实现方法,

案例示图:

201503120101

这次2015-03MC项目有用到,做完了整理一下代码。

{其实关键的东西就是 Cell 的 insert 和 delete,各种判断太麻烦}

1、确认你的 UITableView 的 Delegate 和 DataSource 都已经设置好,SB里面连线或者像下面简单的代码,无所谓了。

self.tableview.delegate = self;
self.tableview.datasource = self;

2、Property

用于存储弹出 Cell 的 IndexPath 位置,还可以用是否为 nil 判断是否存在

@property NSIndexPath *in_lineCellIndexPath;

3、会单独用到的一些方法,
#pragma mark - Support for the in-line cell

//用于判断是否出现
- (BOOL)in_lineCellIsShown {
    return self.in_lineCellIndexPath != nil;
}

//执行 Cell 删除,也可以说是隐藏
- (void)hideExistingPicker {
    [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.in_lineCellIndexPath.row inSection:0]]
                                 withRowAnimation:UITableViewRowAnimationFade];
    
    self.in_lineCellIndexPath = nil;
}

- (NSIndexPath *)calculateIndexPathForNewPicker:(NSIndexPath *)selectedIndexPath {
    
    NSIndexPath *newIndexPath;
    
    if (([self in_lineCellIsShown]) && (self.in_lineCellIndexPath.row < selectedIndexPath.row)){
        
        newIndexPath = [NSIndexPath indexPathForRow:selectedIndexPath.row - 1 inSection:0];
        
    }else {
        
        newIndexPath = [NSIndexPath indexPathForRow:selectedIndexPath.row  inSection:0];
        
    }
    
    return newIndexPath;
}

- (void)showNewCellAtIndex:(NSIndexPath *)indexPath {
    
    NSArray *indexPaths = @[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]];
    
    [self.tableView insertRowsAtIndexPaths:indexPaths
                                 withRowAnimation:UITableViewRowAnimationFade];
}


4、你的Table view dataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//Your Code…
 NSInteger numberOfRows  = ——Yours number, I Don’t care————;
    if ([self in_lineCellIsShown]) {        
        numberOfRows++;
    }
    return numberOfRows;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell;
    if([self in_lineCellIsShown] && (self.in_lineCellIndexPath.row == indexPath.row))
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"in_lineCell"];
        if ( cell == nil){
            cell = [[SWRevealTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"in_lineCell"];
        }
        [cell setBackgroundColor:[UIColor clearColor]];
    }
    return cell;
}


5、你的Table view delegate.

#pragma mark - Table view delegate.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView beginUpdates];
    
    if([self in_lineCellIsShown] && (self.in_lineCellIndexPath.row - 1 == indexPath.row)){
        
        [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.in_lineCellIndexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
        
        self.in_lineCellIndexPath = nil;
        
    }else{
        
        NSIndexPath *newIn_lineCellIndexPath = [self calculateIndexPathForNewPicker:indexPath];
        
        if([self in_lineCellIsShown]){
            
            [self hideExistingPicker];
            
        }
        
        [self showNewCellAtIndex:newIn_lineCellIndexPath];
        
        self.in_lineCellIndexPath = [NSIndexPath indexPathForRow:newIn_lineCellIndexPath.row + 1 inSection:0];
        
    }
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.tableView endUpdates];
    
}


大概大体的代码框架就是这样。

有问题请反馈让我知道,或者有更好的方法,或者你觉得什么不爽的,请留言让我知道。没有成品的Demo。

这篇以后有空会 update,今天很忙,有点儿赶。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值