【问题标题】:How to embed a UITableView in a UIScrollview如何在 UIScrollview 中嵌入 UITableView
【发布时间】:2012-05-17 12:31:20
【问题描述】:

如何在 UIViewController 中执行以下操作(包含 tableview 作为子视图)

我最初有一个 UIViewController 显示预览部分 (UIView)

//Setup container for preview section
UIView *tempContainer = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 320, 100)];
self.preview_answer_container = tempContainer;
self.preview_answer_container.backgroundColor = [UIColor clearColor];
[tempContainer release];
[self.view addSubview:self.preview_answer_container];

我还在下面添加了一个 UITableView(和一个搜索栏)

//setup tableview
UITableView *tempTable = [[UITableView alloc]initWithFrame:CGRectMake(0,44 ,320,self.view.frame.size.height - 44)];
self.tagFriendsTableView = tempTable;
self.tagFriendsTableView.delegate = self;
self.tagFriendsTableView.dataSource = self;
[tempTable release];

[self.view addSubview:self.tagFriendsTableView];

使用此设置,我的滚动区域很小(预览部分下方只有静态区域)

如何修改我的代码 1)我可以向上滚动整个页面,即预览部分和表格视图将一起向上滚动 2)当搜索栏到达顶部(导航栏下方)时,整体滚动将停止(见截图),但UITableView中的内容仍然可以滚动

编辑:

为 UIScroll 视图添加了代码。然而,对我来说什么都没有改变。我的代码有什么问题?

//setup scroll view
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

//Search
UISearchBar *tempBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 120 + 20, 320, 44)];
self.sBar = tempBar;
[tempBar release];
self.sBar.delegate = self;
self.sBar.tintColor = [UIColor colorWithHexString:@"#b6c0c7"];
self.sBar.placeholder = @"Search for FB and DM friends";

[scroll addSubview:sBar];

//setup tableview
UITableView *tempTable = [[UITableView alloc]initWithFrame:CGRectMake(0,144 + 20 + 20 ,320,self.view.frame.size.height - 144 - 20 - 20 - 44)];
self.tagFriendsTableView = tempTable;
self.tagFriendsTableView.delegate = self;
self.tagFriendsTableView.dataSource = self;
[tempTable release];

//Add the scroll view to the parent view
[scroll addSubview:self.tagFriendsTableView];

scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];

【问题讨论】:

    标签: ios uitableview uiviewcontroller uiscrollview scroll


    【解决方案1】:

    我有一个解决方案,

    只用tableView就够了

    1. 将“预览视图”设置为 tableView 的 HeaderView

    2. 将“搜索栏”设为 tableView 的 Section Header View

    你可以做到完美:)

    【讨论】:

    • 任何机构可以再为我解释一下吗?请
    • @RaviOjha,看看下面 DBD 的答案。
    【解决方案2】:

    不需要任何花哨或自定义,使用UITableView 并将您的预览窗格设置为tableHeaderView,并将您的搜索字段设置为使用tableView:viewForHeaderInSection:UITableViewDelegate 中的部分标题。

    在滚动事件期间,表格标题将滚动到顶部。 如果可见,部分标题会在该部分的整个时间内浮动并保持可见。如果您只有 1 个部分,则部分标题将一直存在。

    【讨论】:

      【解决方案3】:

      或者您可以执行以下步骤: 1.禁用tableView的滚动。 2. 为 tableView 设置高度约束和一个连接到它的 IBOutlet。 3. 在重新加载数据之前,计算表格高度。 heightConstraint.constant = (heightOfTheSingleTableViewCell) * numOfRows

      【讨论】:

        【解决方案4】:

        您必须将 UITableView 嵌入到 UIScrollView 中,该 UIScrollView 的屏幕大小减去导航栏。将 UIScrollViews 背景设置为 clearColor 以允许 UITableView 上方的内容保持可见。

        【讨论】:

        • 您好,感谢您的回复。我为 UIScroll 视图添加了一些代码,但它对我不起作用,即与我的原始实现没有任何变化。你能帮我看看我编辑的问题吗?我在那里添加了我的 uiscrollview 代码。提前致谢
        • 您实际上应该使用 DBD/adali 的解决方案,它比我的要好:)
        【解决方案5】:

        是的,您可以使用“tableView:viewForHeaderInSection”。

        【讨论】:

          【解决方案6】:
          scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
          

          无法滚动,因为 contentSize 不够大。您需要将滚动视图顶部的内容的长度增加到总长度。

          【讨论】:

            猜你喜欢
            • 2015-08-12
            • 1970-01-01
            • 1970-01-01
            • 2012-07-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-09
            相关资源
            最近更新 更多