繁体   English   中英

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

[英]Adding buttons to top and bottom of UITableView

在回合制游戏中,我有一个屏幕显示我当前正在参加的所有比赛。 我为此使用3个部分的表视图。

现在,我想在显示游戏上方的顶部添加一个按钮“开始新游戏”。 在显示游戏的下方,我需要一些按钮,例如“商店”等。我可以放置它们,但它们不会滚动,因此我需要将它们放在表格视图中,以便它们滚动。

最好的方法是什么?

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;

我不确定您想要按钮的确切位置,但是UITableView有一个
@property(nonatomic, retain) UIView *tableHeaderView
如果希望它位于tableView的顶部并与之滚动,则只需创建所需的视图并将其放在该属性中即可。

UITableView具有tableHeaderView和tableFooterView属性。 您可以为自定义视图设置此值。

@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

注意:当您将自定义视图设置为tableHeaderView / tableFooterViewn属性时,其宽度将更改为与表视图的宽度相同

将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)

您的问题不是很清楚..在表视图中,我们给了属性property(nonatomic,retain)UIView * tableHeaderView;
@property(nonatomic,retain)UIView * tableFooterView;

甚至你都可以结帐

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

您也可以在此处添加视图。

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

您可以使用这两个部分之间的高度

暂无
暂无

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

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