簡體   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