我想在我的表格 View 上方制作一个小播放器选项卡,因此它应该始终隐藏,只有一个小的 1px 字符串可见,就像在笔记应用程序中一样,当您滚动到顶部时,它应该出现,但保持在顶部,而不是向下,就像表格 View 中的所有单元格一样。 它不能是标题,因为它始终可见,并且它不能是单元格,因为它必须位于顶部。 所以我想做所有事情就像在笔记应用程序中一样,我该怎么做? 谢谢
最佳答案
这是我的想法。
- 添加
UITableView
和UIImageView
作为底层UIView
的 subview 。UIImageView
实例是“字符串/条纹”的表示(无论你怎么调用它) - 创建两个单元原型(prototype)。一种用于普通单元格,一种用于标题单元格。
- 对于第一个单元格 (
indexPath.row == 0
),请始终使用标题单元格原型(prototype)。对于其他细胞,使用正常细胞原型(prototype)。 - 在
viewWillAppear
中,始终使用[UITableViewscrollToRowAtIndexPath:atScrollPosition:animated:] 滚动到第二行
下面是我的示例代码片段。 (至少按照我的预期工作了)
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
self.overlayView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stripe.png"]]; // This is an image with stripe with transparent background
self.overlayView.frame = CGRectMake(0, 0, 320, 10);
[self.view addSubview:self.overlayView];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// It is important that the table has enough rows to scroll. Otherwise, [UITableView scrollToRowAtIndexPath...] will not take effect.
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell;
if(indexPath.row == 0)
{
// This is an example of header cell
cell = [tableView dequeueReusableCellWithIdentifier:@"topCell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"topCell"];
[cell.textLabel setText:@"This is the play tab"];
}
}
else
{
// This is an example of normal cell
cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"normalCell"];
}
[cell.textLabel setText:[NSString stringWithFormat:@"Data at : %d", indexPath.row - 1]];
[cell.detailTextLabel setText:@"A normal cell."];
}
return cell;
}
关于objective-c - 在表格 View 中制作淡入淡出的标题,就像在笔记应用程序中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987687/