【发布时间】:2017-11-02 16:47:23
【问题描述】:
我使用 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
【问题讨论】:
-
你有多少个
UITableView?而且我找不到每个单元格上的按钮在哪里。 -
我现在只有一个 UITableView 但是一旦我让这部分工作,我想实现多个。我不确定我是否理解有关单元格按钮的问题。
-
你的下拉列表是什么?
the button on a cell我的意思是我看不到你的手机并按下按钮;) -
我的下拉列表是viewDidLoad中定义的数组。
-
不,我的意思是什么视图显示你的数组
标签: ios objective-c