如何在按钮上单击打开和关闭tableView?

时间:2017-08-03 09:23:03

标签: ios objective-c uitableview uibutton

enter image description here

2我正在使用dropDowns,我正在动态创建按钮和下拉列表。当我点击第一个按钮时,最后一个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;
            }

}

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

1 个答案:

答案 0 :(得分: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;
            }

}