objective-c - 在表格 View 中制作淡入淡出的标题,就像在笔记应用程序中一样

标签 objective-c ios uitableview

我想在我的表格 View 上方制作一个小播放器选项卡,因此它应该始终隐藏,只有一个小的 1px 字符串可见,就像在笔记应用程序中一样,当您滚动到顶部时,它应该出现,但保持在顶部,而不是向下,就像表格 View 中的所有单元格一样。 它不能是标题,因为它始终可见,并且它不能是单元格,因为它必须位于顶部。 所以我想做所有事情就像在笔记应用程序中一样,我该怎么做? 谢谢

最佳答案

这是我的想法。

  1. 添加 UITableViewUIImageView 作为底层 UIView 的 subview 。 UIImageView 实例是“字符串/条纹”的表示(无论你怎么调用它)
  2. 创建两个单元原型(prototype)。一种用于普通单元格,一种用于标题单元格。
  3. 对于第一个单元格 (indexPath.row == 0),请始终使用标题单元格原型(prototype)。对于其他细胞,使用正常细胞原型(prototype)。
  4. 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/

相关文章:

ios - ScrollView 的MVC

iphone - tableheaderview 滚动事件

objective-c - obj-c 两个数字之间的线性插值

ios - 按下主页按钮后,录音机滚轮动画不起作用

ios - 下载的模拟器在 macOS 10.15 上的 Xcode 10.1 中不显示

iphone - 将导航 Controller 添加到现有的 UITableView

ios - 使用 Storyboard的多节 UITableView

iphone - 如何在XCode(iOS SDK)中添加按钮到注释 View

ios - 带有静态单元格的 TableView "...dataSource must return a cell from cellForRowAtIndexPath"错误

objective-c - 根据 Apple 指南,将文件保存到哪个目录?