gpt4 book ai didi

ios - 由 UITableView 制作的下拉按钮不重叠按钮

转载 作者:行者123 更新时间:2023-11-28 21:03:26 26 4
gpt4 key购买 nike

我使用 TableView 实现了一个下拉按钮,当用户单击该按钮时,会在按钮底部弹出一个下拉列表。到目前为止,此功能运行良好。但是,当我在第一个按钮的底部添加另一个按钮并单击下拉列表时,tableView 不会与底部的按钮重叠。它没有出现在上面,而是出现在下面。

我的问题是如何在单击下拉按钮时让 tableView 出现在第二个按钮的顶部。我在下面插入了我的代码。

#
import < UIKit / UIKit.h >

@interface ViewController: UIViewController < UITableViewDataSource, UITableViewDelegate >

@property(weak, nonatomic) IBOutlet UIButton * categoriesBtn;
@property(weak, nonatomic) IBOutlet UITableView * categoriesTable;

@property(strong, nonatomic) NSArray * data;
-(IBAction) categoriesButton: (id) sender;
-(IBAction) submit: (id) sender;

@end

@implementation ViewController

-(void) viewDidLoad {
[super viewDidLoad];

[self.categoriesBtn setTitle: @ "+ Categories" forState: UIControlStateNormal ];
self.data = [[NSArray alloc] initWithObjects: @ "Value1", @ "Value2", @ "Value3", @ "Value4", @ "Value5", nil ];
self.categoriesTable.delegate = self;
self.categoriesTable.dataSource = self;
self.categoriesTable.hidden = YES;

}

-(NSInteger) tableView: (UITableView * ) tableView numberOfRowsInSection: (NSInteger) section {

return [self.data count];

}

-(UITableViewCell * ) tableView: (UITableView * ) tableView cellForRowAtIndexPath: (NSIndexPath * ) indexPath {

static NSString * simpleTableIdentifier = @ "SimpleTableItem";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: simpleTableIdentifier];
}
cell.textLabel.text = [self.data objectAtIndex: indexPath.row];

return cell;
}

-(IBAction) categoriesButton: (id) sender {
if (self.categoriesTable.hidden == YES) {
self.categoriesTable.hidden = NO;
} else
self.categoriesTable.hidden = YES;
}


-(void) tableView: (UITableView * ) tableView didSelectRowAtIndexPath: (NSIndexPath * ) indexPath {

UITableViewCell * cell = [self.categoriesTable cellForRowAtIndexPath: indexPath];
[self.categoriesBtn setTitle: cell.textLabel.text forState: UIControlStateNormal];
self.categoriesTable.hidden = YES;

}

-(void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

@end

enter image description here

enter image description here

最佳答案

要使 categoriesTablequeryTypeButton 重叠,您应该更改 ViewController 的 subview 的顺序,如下图所示。按照此顺序,categoriesButton 位于按钮下方,因此当它显示在屏幕上时,它将位于屏幕顶部( View 树)。

enter image description here

或者每次单击 categoriesButton 时,您都应该将 categoriesTable 放在前面。如果这样做,您的 categoriesTable 将在出现时与所有按钮重叠。像这样更改您的 categoriesButton 方法

-(IBAction)categoriesButton: (id) sender {
[self.view bringSubviewToFront:self.categoriesTable];

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

关于ios - 由 UITableView 制作的下拉按钮不重叠按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47080543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com