【问题标题】:How to identify the section number of a section header view如何识别节标题视图的节号
【发布时间】:2013-06-01 11:47:59
【问题描述】:

UITableView 的标题中有一个按钮。一旦按钮被触摸到里面,我们怎么知道按钮属于哪个部分?由于 tableview 是可编辑的,因此当删除某些行时,设置按钮的标签不是很好。我尝试使用indexPathForRowAtPoint: 来获取第一行的 indexPath 属于该部分,但发生了一些奇怪的事情。有没有更好的办法?

编辑 1:

一旦标签被用于标识标题的节号,当某行被删除时,标签将不会更新。因为你可以重新加载 tableview 来更新标签,但这似乎不是那么好。

对于indexPathForRowAtPoint:的怪异行为,我再发一个问题:Weird behavior of UITableView method "indexPathForRowAtPoint:"

【问题讨论】:

  • headerview 没有关联到一个部分。您指的是节标题吗?
  • 对不起,我的意思是节标题。
  • 不知道为什么这个问题被投票,因为它不完整。可编辑的表格会使标签不起作用怎么办?使用 indexPathForRowAtPoint 发生了什么“奇怪的事情”?代码在哪里?创建标题和按钮的代码在哪里?
  • @danh 我发布了一个问题来解释“奇怪的事情”stackoverflow.com/questions/16946029/…
  • @danh 请检查更新

标签: ios uitableview


【解决方案1】:

我喜欢@LuisCien 的回答,因为 OP 想要避免使用标签。但是(a)无论在标题视图的层次结构中找到按钮有多深,答案都应该显示如何从按钮到该部分,并且(b)提供的答案将第 0 部分与未找到标题的情况混为一谈(如果方法传递了一个不包含在标题中的视图)。

// LuisCien's good suggestion, with modified test and a NotFound return.

-(NSInteger)sectionNumberForView:(UIView*)view inTableView:(UITableView*)tableView {

    NSInteger numberOfSections = [tableView numberOfSections];

    for(NSInteger i=0; i < numberOfSections; ++i) {
        UIView *headerView = [tableView headerViewForSection:i];
        if ([view isDescendantOfView:headerView]) return i;
    }

    return NSNotFound;
}

无需使用插座的超级视图调用它。让后代检查完成这项工作。

NSInteger section = [self sectionNumberForView:sender inTableView:_yourTableView];

【讨论】:

【解决方案2】:

上面的答案对我来说似乎已经足够好了,但是,如果您不想使用标签,您可以创建一个方法来返回特定视图所属的 UITableView 部分,如下所示:

-(int)sectionNumberForView:(UIView*)view inTableView:(UITableView*)tableView {

    int numberOfSections = [tableView numberOfSections];

    int i=0;
    for(; i < numberOfSections; ++i) {
        UIView *headerView = [tableView headerViewForSection:i];
        if (headerView == view) {
            break;
        }
    }

    return i;
}

然后在您的 Target-Action 方法中并假设您的按钮的超级视图是节标题视图:

-(void)buttonPressed:(UIButton*)sender {

    int section = [self sectionNumberForView:sender.superview inTableView:_yourTableView];
}

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    在创建每个 headerView 并添加 UIButton 时,您可以将其标签设置为该部分的值,并在按钮的操作方法中检查该标签。比如……

    在你的创作中:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                button.frame = CGRectMake(10, 10, 20, 20);
                [button setTag:section];
                [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
        [view addSubview:button];
    
        return view;
    }
    

    然后在你的操作方法中:

    - (void)buttonPressed:(UIButton *)sender
    {
        int section = sender.tag;
    
        // Do something based on the section
    }
    

    【讨论】:

    • 但此代码存在问题中所述的问题。该表是可编辑的。随着其他部分的添加或删除,与标题关联的部分编号会随时间而变化。使用 indexPath 部分或行作为标记值仅适用于具有固定部分和行的表。
    • 我在他编辑之前写了这篇文章,所以需要一些额外的工作才能完成
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    相关资源
    最近更新 更多