简体   繁体   English

在UITableView的顶部和底部添加按钮

[英]Adding buttons to top and bottom of UITableView

I have a screen displaying all the matches I'm currently in, in my turn-based game. 在回合制游戏中,我有一个屏幕显示我当前正在参加的所有比赛。 I am using a table view with 3 sections for this. 我为此使用3个部分的表视图。

Now I want to add a button 'start new game' to the top above the displaying games. 现在,我想在显示游戏上方的顶部添加一个按钮“开始新游戏”。 Below the displaying games I want a few buttons like 'shop' etc. I can place them but then they won't scroll along so I need to put them in the tableview so they scroll along. 在显示游戏的下方,我需要一些按钮,例如“商店”等。我可以放置它们,但它们不会滚动,因此我需要将它们放在表格视图中,以便它们滚动。

What would be the best way to do this? 最好的方法是什么?

UIView *headerContainer = [[UIView alloc]initWithFrame:CGRectMake(X, X, X, X)];
UIButton *btnHeader = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[headerContainer addSubview:btnHeader];


UIView *footerContainer = [[UIView alloc]initWithFrame:CGRectMake(X, X, X, X)];
UIButton *btnFooter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[footerContainer addSubview:btnFooter];


tblView.tableHeaderView = headerContainer;
tblView.tableFooterView = footerContainer;

I'm not sure exactly where you want your button, but UITableView have a 我不确定您想要按钮的确切位置,但是UITableView有一个
@property(nonatomic, retain) UIView *tableHeaderView
If you want it to be on the top of your tableView and scroll with it, just make the view you want and put it in that property. 如果希望它位于tableView的顶部并与之滚动,则只需创建所需的视图并将其放在该属性中即可。

UITableView has a tableHeaderView and a tableFooterView property. UITableView具有tableHeaderView和tableFooterView属性。 You can set this value for your custom view. 您可以为自定义视图设置此值。

@property(nonatomic,retain) UIView *tableHeaderView;                            // accessory view for above row content. default is nil. not to be confused with section header
@property(nonatomic,retain) UIView *tableFooterView;                            // accessory view below content. default is nil. not to be confused with section footer

Note: when you set your custom view to tableHeaderView/tableFooterViewn property, its width will be changed as wide as table view's 注意:当您将自定义视图设置为tableHeaderView / tableFooterViewn属性时,其宽度将更改为与表视图的宽度相同

add UIbutton to Navigation controller view like this code: 将UIbutton添加到导航控制器视图中,如下代码:

let createButton = UIButton.init(frame: CGRect.init(x: 0, y: self.view.frame.height-50, width: self.view.frame.width, height: 50))
createButton.backgroundColor = UIColor.orange
createButton.setTitle("Create", for: UIControlState())
createButton.addTarget(self, action: #selector(didTapCreate(_:)), for: .touchUpInside)
self.navigationController?.view.addSubview(createButton)

Your question is not that much clear.. in tableview we we gave a propertys property(nonatomic,retain) UIView *tableHeaderView; 您的问题不是很清楚..在表视图中,我们给了属性property(nonatomic,retain)UIView * tableHeaderView;
@property(nonatomic,retain) UIView *tableFooterView; @property(nonatomic,retain)UIView * tableFooterView;

and even you can checkout this 甚至你都可以结帐

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

you can added your view here too. 您也可以在此处添加视图。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

this you can use for the height between the sections 您可以使用这两个部分之间的高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM