【问题标题】:Implementing uitableview with change button inside uiscrollview在uiscrollview中使用更改按钮实现uitableview
【发布时间】:2013-04-28 16:29:31
【问题描述】:

我查看了这些问题,但找不到我想要的确切内容。 UITableView and Another View inside ScrollView

Programmatically force a UIScrollView to stop scrolling, for sharing a table view with multiple data sources

我正在尝试执行以下操作: 在 UIScrollView 中,有一个带有几个控制器的 UIView。

在它们下方,类似于 UISegmentedControl。下面应该是 UITableView。 UISegmentedControl 会切换 UITableView 的内容。

所有这些都在 UIScrollView 中。

我想要实现的是当我向下滚动时,我希望 UISegmentedControl 保持在视图的顶部。如果我继续向下滚动,只会滚动 UITableView 的内容。

这可能吗? 谢谢!

【问题讨论】:

  • 但是为什么你需要一个 UIScrollView?您所描述的是位于 UITableView 上方的分段控件 - 没有包含它们的 UIScrollView。
  • 但在分段控件之上,我还有一些我想成为滚动条的控件。

标签: ios uitableview uiscrollview


【解决方案1】:

假设我理解您的要求,我会按以下方式执行此操作:

  1. 使用一个带有两个部分的 UITableView
  2. 将“一对控制器”放在表格的第一部分。由您决定(无论是在单个单元格中还是在其他任何地方)。您只需指示 UITableViewDataSource 在请求第一部分的内容时返回这些控件
  3. 在 UITableViewDelegate 方法中
    (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    如果该部分是 1 ,则构建一个包含分段控件的视图并将其返回
  4. 每当向数据源询问 second 部分的内容时,检查分段控件的状态并返回正确的数据
  5. 每当分段控件发生变化时,捕捉事件并重新加载表格视图。这样,第 4 步将提供在第二部分中显示的新数据,并且表格将更改内容

这里的技巧是 UITableView 部分标题在滚动时会粘在表格的顶部,因此您将准确地实现您正在搜索的内容。 表格的第一部分将滚开,分段控制器将粘在顶部,而第 2 部分将继续在其下方滚动。

【讨论】:

  • 第一节的表头怎么办?因为它会一直停留在视图的顶部。
  • 现在不记得了,但可能是你必须实现 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section,为第一部分指定高度 0 和高度第二个的分段控制。万一告诉我我更新了答案..
【解决方案2】:

我已经实现了一个包含 2 个部分的 UITableView。第一部分没有标题,标题的高度为0。

第一部分的第一个也是唯一一个单元格是我想要滚动的 UIView(红色背景)。第二部分的标题是 UISegmentedControl(绿色背景)。

- (void)viewDidLoad {
    [super viewDidLoad];

    data = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6",@"7", @"8", @"9",@"10",nil];

    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

    table.delegate = self;
    table.dataSource = self;

    [self.view addSubview:table];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
    //Return zero for the first section's header.
    if (section == 0)
        return 0;
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //Return nil for the first section's header view.
    if (section == 0) return nil;

    head1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    head1.backgroundColor = [UIColor greenColor];

    return head1;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) return 1;

    return data.count;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //First section - put the UIView that will be scrollable
    if (indexPath.section == 0)
    {
        static NSString *CellIdentifier = @"ControlCell1";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        head.backgroundColor = [UIColor redColor];
        [cell.contentView addSubview:head];

        return cell;
    }

    static NSString *CellIdentifier = @"ControlCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }   
    cell.textLabel.text = [data objectAtIndex:indexPath.row];
    return cell;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 2016-09-12
    • 2012-10-08
    • 2012-04-04
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多