ios - UITableViewCell 滚动自定义 UITableView 标题标题

标签 ios objective-c uitableview

我有一个简单的 UITableViewController,它包含大约 40 个 UITableViewCellsUITableViewController 嵌入到 UITabBarController 中。 UITableViewController 在 Storyboard 中创建。

我正在覆盖自定义 UITableViewHeader 文本,以便我可以为用户提供一些指南。我注意到一些非常奇怪的行为,如下图所示:

enter image description here

enter image description here

可以看出,UITableView 滚动了,但标题文本仍保留在那里,看起来很糟糕。

这是我设置自定义 header 的方式:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // Custom label for the section header titles
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, tableView.frame.size.width, 110)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;

    [label setFont:[UIFont systemFontOfSize:17.0]];

    if (section == 0)
    {
        label.text = @" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
    }    
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    CGFloat headerHeight = 0;

    headerHeight = 110;

    return headerHeight;

}

我使用过不同的尺寸,但无论我做什么,它的行为方式仍然相同。

如果有人对此有任何想法或指导,那将不胜感激。我只希望标题文本也随着表格 View 向上滚动,因此在滚动时从 View 中隐藏。

最佳答案

您可以像这样将标题添加为 tableHeaderView:

- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, self.tableView.frame.size.width, 110)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    label.numberOfLines = 0;
    [label setFont:[UIFont systemFontOfSize:17.0]];
    label.text = @" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
    self.tableView.tableHeaderView = label;
}

关于ios - UITableViewCell 滚动自定义 UITableView 标题标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417449/

相关文章:

objective-c - 基本的 Facebook/Twitter URL 处理程序格式

iphone - 有没有办法阻止从 textview 复制/粘贴?

ios - 通过使用拖动委托(delegate)向上或向下拖动一行来重新排列表格中的部分(iOS 11)

iphone - UITableView使用CALayer + mask的滚动性能

ios - 用一组 NSManagedObjects 覆盖现有的核心数据

ios - 为什么页眉和页脚在删除时会跟随行的移动?

ios - 类型的值没有成员

objective-c - 设置 frame.origin 与 CGAffineTransformMakeTranslation 之间的性能差异

ios - 从解析中检索用户数据以快速显示在 TableView 中

ios - 快速翻转 View : issues with whole screen flipping + tap gesture only works twice