【问题标题】:How to open and close tableView on a button click?如何在单击按钮时打开和关闭 tableView?
【发布时间】:2018-01-10 19:29:11
【问题描述】:

2我正在处理下拉菜单,我正在动态创建按钮和下拉表格视图。当我单击第一个按钮时,最后一个 tableView 被打开。但我想打开特定的 tableView(一次我只想打开一个下拉菜单)。

float y = 0;
    float x = (self.answersView.frame.size.width/2)+175;

    float Iy = 0;
    float Ix = 675;

    float Tx = (self.answersView.frame.size.width/2)+175;
    float Ty = 0;

    for (int i=0; i<self.radioBtnTagArr.count; i++) {

        self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
        [self.dropdownBtn setTitle:@"0" forState:UIControlStateNormal];
        self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
        self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];

        [self.dropdownBtn addTarget:self action:@selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
        self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:self.dropdownBtn];
        self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue];

        y = y+70;

        self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
        self.dropdownImageView.image = [UIImage imageNamed:@"dropdown.png"];
        [self.answersView addSubview:self.dropdownImageView];

        Iy = Iy+70;

        self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
        self.content = @[ @"Monday", @"Tuesday", @"Wednesday",@"Thursday",@"Friday",@"Saturday",@"Sunday"];
        self.dropdownTableView.delegate = self;
        self.dropdownTableView.dataSource = self;
        [self.answersView addSubview:self.dropdownTableView];

        Ty = Ty+70;

        self.dropdownTableView.hidden = YES;

    }

//事件处理方法

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


            if (self.dropdownTableView.hidden == YES) {
                self.dropdownTableView.hidden = NO;
            } else {
                self.dropdownTableView.hidden = YES;
            }

}

以及如何动态设置表格视图高度。

【问题讨论】:

  • 你可以分享预期视图的屏幕截图吗?
  • 什么是 answersView 以及这个视图是什么?
  • answersView 是 mainView 中的一个子视图。它也有滚动视图。
  • 您可以使用 tableview 制作此视图。名称和图像添加为部分。单击部分,您可以隐藏和显示行。
  • 当我单击第一个 btn 时,我只想打开第一个表,当我再次单击第一个 btn 时,我想关闭它。像这样我想要所有的包子......

标签: ios objective-c uitableview cocoa-touch uibutton


【解决方案1】:

试试这个:

NSMutableArray *arrTableview; // make global

在 ViewDidLoad 中

  arrTableview = [NSMutableArray New];

改变以下方法

for (int i=0; i<self.radioBtnTagArr.count; i++) {

        self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        self.dropdownBtn.tag = i; // add this line
        self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
        [self.dropdownBtn setTitle:@"0" forState:UIControlStateNormal];
        self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
        self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];

        [self.dropdownBtn addTarget:self action:@selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
        self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:self.dropdownBtn];
        //self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue]; // This line commented.

        y = y+70;

        self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
        self.dropdownImageView.image = [UIImage imageNamed:@"dropdown.png"];
        [self.answersView addSubview:self.dropdownImageView];

        Iy = Iy+70;

        self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
        self.content = @[ @"Monday", @"Tuesday", @"Wednesday",@"Thursday",@"Friday",@"Saturday",@"Sunday"];
        self.dropdownTableView.delegate = self;
        self.dropdownTableView.dataSource = self;
        [self.answersView addSubview:self.dropdownTableView];
        [arrTableview addObject :self.dropdownTableView]; 
        Ty = Ty+70;

        self.dropdownTableView.hidden = YES;

    }

选择器

- (void)dropdownBtnTapMethod:(UIButton *) sender {
     UITableView *tableView =[arrTableview objectAtIndex:sender.tag]

            if (tableView.hidden == YES) {
                tableView.hidden = NO;
            } else {
                tableView.hidden = YES;
            }

}

【讨论】:

  • UITableView *tableView =[arrTableview objectAtIndexPath:sender.tag] 在这里您将 sender.tag 询问到 arrTableView,但我没有看到可见的界面 objectAtIndexPath:
  • 这有什么问题吗?
  • 我收到错误“NSMutableArray”没有可见界面声明选择器“objectAtIndexPath:”
  • 我有更新的答案。检查您是否需要从数组中获取 tableview 的引用。
  • 我删除了路径形式 objectAtInedxPath: ,我添加了这样...UITableView *tableView = [self.arrTableview objectAtIndex:sender.tag];但我得到了例外。例外是.. 索引 90 超出空数组的范围。
猜你喜欢
  • 2014-04-07
  • 2019-12-09
  • 1970-01-01
  • 2020-06-03
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 2014-12-22
  • 2015-04-07
相关资源
最近更新 更多