将UIButton添加到UITableView节头

 mobiledu2502936451 发布于 2023-02-08 02:27

我有一个UITableView1节和节标题,我想保持标题的一切相同,但只需在右侧添加一个按钮.我无法将按钮放在导航控制器中,因为放置这样一个按钮的两个可用位置已被其他按钮占用.

在这里你可以看到我试图做的结果.现在,我想要做的就是在标题的右侧添加一个添加按钮,但我尝试过的却没有用.我还尝试了StackOverflow上的其他一些解决方案,但是他们并没有完成我想做的事情.此外,如果有更好的方法来创建添加按钮,请告诉我.我尝试使用a UIBarButtonItem但我无法弄清楚如何将其视图添加到实际视图中.谢谢!

在此输入图像描述

这是我到目前为止在我的代表中所做的事情:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *addButton = [[UIButton alloc] init]; //I also tried setting a frame for the
    button, but that did not seem to work, so I figured I would just leave it blank for posting       
    the question.
    addButton.titleLabel.text = @"+";
    addButton.backgroundColor = [UIColor redColor];
    [self.tableView.tableHeaderView insertSubview:addButton atIndex:0]; 
    //I feel like this is a bad idea

    return self.tableView.tableHeaderView;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

Yas T... 25

问题是你现在self.tableView.tableHeaderViewnil处于这个时间点,因此你无法使用它.所以你需要做的是,创建一个UIView,添加标题和按钮,设置样式并返回它.

这应该为您的节标题添加标题和按钮,您仍然需要使用正确的字体和大小设置标题样式,但会给您一个想法.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGRect frame = tableView.frame;

    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];
    [addButton setTitle:@"+" forState:UIControlStateNormal];
    addButton.backgroundColor = [UIColor redColor];

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    title.text = @"Reminders";

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [headerView addSubview:title];
    [headerView addSubview:addButton];

    return headerView;
}


mwl.. 13

从iOS 6开始,您也可以实施

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

在你的UITableViewDelegate.例如:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if (section == 0) {
        if ([view.subviews.lastObject isKindOfClass:[UIButton class]]) {
            return;
        }
        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(view.frame.size.width - 160.0, 0, 160.0, view.frame.size.height); // x,y,width,height
        [button setTitle:@"My Button" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(sectionHeaderButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
    }
}


Lubos.. 6

如果您对Swift解决方案感兴趣:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let frame = tableView.frame

    let button = UIButton(frame: CGRectMake(5, 10, 15, 15))  // create button
    button.tag = section 
    // the button is image - set image
    button.setImage(UIImage(named: "remove_button"), forState: UIControlState.Normal)  // assumes there is an image named "remove_button"
    button.addTarget(self, action: #selector(TestController.remove(_:)), forControlEvents: .TouchUpInside)  // add selector called by clicking on the button

    let headerView = UIView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))  // create custom view
    headerView.addSubview(button)   // add the button to the view

    return headerView   
}

您可能希望在该部分中指定标题的高度.这必须与自定义视图的高度同步:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return CGFloat(30)
}


tiritea.. 5

您有2个选择,都可以通过选择tableView.delegate。第一个涉及实现委托方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

这样一来,您基本上可以在自定义视图中返回所需的任何内容,并将其用作所需节的标题。这可能很简单

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [addButton addTarget:self action:@selector(SomeMethod:) forControlEvents:UIControlEventTouchUpInside];
    return addButton;
}

这会将一个圆形的信息按钮(居中)作为标题。但是,您更有可能想要创建一个实际的UIView对象,并使用按钮(和节标题标签?)填充它,并按自己的意愿进行布局(请参阅其他人的答案,以了解具体方法)。

但是,对于一般的情况,第二种方法可能会更好,这通常是向部分标题中的一个添加按钮(我知道您说了1个部分,但是很多人可能会想在此问题上进行类似的操作)多节表]。这涉及实现委托方法

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

并在事后将按钮添加到标题中;特别

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Remove old button from re-used header
    if ([view.subviews.lastObject isKindOfClass:UIButton.class])
        [view.subviews.lastObject removeFromSuperview];

    if (section == MySectionNumberWithButton) {
        UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
        [addButton addTarget:self action:@selector(SomeMethod:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:addButton];

        // Place button on far right margin of header
        addButton.translatesAutoresizingMaskIntoConstraints = NO; // use autolayout constraints instead
        [addButton.trailingAnchor constraintEqualToAnchor:view.layoutMarginsGuide.trailingAnchor].active = YES;
        [addButton.bottomAnchor constraintEqualToAnchor:view.layoutMarginsGuide.bottomAnchor].active = YES;
    }
}

请注意,由于tableView重用标题,因此在多节表中,必须确保删除以前可能添加的任何按钮,否则最终将在不需要的节中出现按钮。然后,如果这是正确的部分,则将按钮添加到现有的标题中。请注意,NSLayoutAnchor为了简洁起见,我正在使用(iOS 9+)布置按钮;您可以使用NSLayoutConstraint(iOS 6+)进行相同的操作

这种方法的独特优势在于-仅需添加按钮-您无需重新创建常规布局即可匹配所有其他(非按钮)标题,也不必担心旋转设备时页边距会发生变化等。特别是,标题标题保持不变,并且将保留您可能为表格标题定义的所有全局外观设置(例如,字体,颜色,...),如果采用这种tableView:viewForHeaderInSection:方法,您将不得不重新创建所有这些标题。

4 个回答
  • 问题是你现在self.tableView.tableHeaderViewnil处于这个时间点,因此你无法使用它.所以你需要做的是,创建一个UIView,添加标题和按钮,设置样式并返回它.

    这应该为您的节标题添加标题和按钮,您仍然需要使用正确的字体和大小设置标题样式,但会给您一个想法.

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        CGRect frame = tableView.frame;
    
        UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];
        [addButton setTitle:@"+" forState:UIControlStateNormal];
        addButton.backgroundColor = [UIColor redColor];
    
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
        title.text = @"Reminders";
    
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [headerView addSubview:title];
        [headerView addSubview:addButton];
    
        return headerView;
    }
    

    2023-02-08 02:49 回答
  • 从iOS 6开始,您也可以实施

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
    

    在你的UITableViewDelegate.例如:

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        if (section == 0) {
            if ([view.subviews.lastObject isKindOfClass:[UIButton class]]) {
                return;
            }
            UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(view.frame.size.width - 160.0, 0, 160.0, view.frame.size.height); // x,y,width,height
            [button setTitle:@"My Button" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(sectionHeaderButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:button];
        }
    }
    

    2023-02-08 02:50 回答
  • 您有2个选择,都可以通过选择tableView.delegate。第一个涉及实现委托方法

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    

    这样一来,您基本上可以在自定义视图中返回所需的任何内容,并将其用作所需节的标题。这可能很简单

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
        [addButton addTarget:self action:@selector(SomeMethod:) forControlEvents:UIControlEventTouchUpInside];
        return addButton;
    }
    

    这会将一个圆形的信息按钮(居中)作为标题。但是,您更有可能想要创建一个实际的UIView对象,并使用按钮(和节标题标签?)填充它,并按自己的意愿进行布局(请参阅其他人的答案,以了解具体方法)。

    但是,对于一般的情况,第二种方法可能会更好,这通常是向部分标题中的一个添加按钮(我知道您说了1个部分,但是很多人可能会想在此问题上进行类似的操作)多节表]。这涉及实现委托方法

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
    

    并在事后将按钮添加到标题中;特别

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
    {
        // Remove old button from re-used header
        if ([view.subviews.lastObject isKindOfClass:UIButton.class])
            [view.subviews.lastObject removeFromSuperview];
    
        if (section == MySectionNumberWithButton) {
            UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
            [addButton addTarget:self action:@selector(SomeMethod:) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:addButton];
    
            // Place button on far right margin of header
            addButton.translatesAutoresizingMaskIntoConstraints = NO; // use autolayout constraints instead
            [addButton.trailingAnchor constraintEqualToAnchor:view.layoutMarginsGuide.trailingAnchor].active = YES;
            [addButton.bottomAnchor constraintEqualToAnchor:view.layoutMarginsGuide.bottomAnchor].active = YES;
        }
    }
    

    请注意,由于tableView重用标题,因此在多节表中,必须确保删除以前可能添加的任何按钮,否则最终将在不需要的节中出现按钮。然后,如果这是正确的部分,则将按钮添加到现有的标题中。请注意,NSLayoutAnchor为了简洁起见,我正在使用(iOS 9+)布置按钮;您可以使用NSLayoutConstraint(iOS 6+)进行相同的操作

    这种方法的独特优势在于-仅需添加按钮-您无需重新创建常规布局即可匹配所有其他(非按钮)标题,也不必担心旋转设备时页边距会发生变化等。特别是,标题标题保持不变,并且将保留您可能为表格标题定义的所有全局外观设置(例如,字体,颜色,...),如果采用这种tableView:viewForHeaderInSection:方法,您将不得不重新创建所有这些标题。

    2023-02-08 02:55 回答
  • 如果您对Swift解决方案感兴趣:

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        let frame = tableView.frame
    
        let button = UIButton(frame: CGRectMake(5, 10, 15, 15))  // create button
        button.tag = section 
        // the button is image - set image
        button.setImage(UIImage(named: "remove_button"), forState: UIControlState.Normal)  // assumes there is an image named "remove_button"
        button.addTarget(self, action: #selector(TestController.remove(_:)), forControlEvents: .TouchUpInside)  // add selector called by clicking on the button
    
        let headerView = UIView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))  // create custom view
        headerView.addSubview(button)   // add the button to the view
    
        return headerView   
    }
    

    您可能希望在该部分中指定标题的高度.这必须与自定义视图的高度同步:

    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat(30)
    }
    

    2023-02-08 02:57 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有