这个案例最先是在 iOS 官方的应用 — Calendar 中有一个In_line Date Picker的效果,其实就是 EKEventKit 中的 EKEventEditViewController 也包含了这个 In_line 的效果。
这里写一下实现方法,
案例示图:
这次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,今天很忙,有点儿赶。