2012-09-17 83 views
0

的鋒網越來越設定我編程方式添加一個按鈕,一個UITableViewController這樣的:按鈕添加到的UITableViewController中的tableView

-(void)createFlipButton 
{ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark]; 
    button.frame = CGRectMake(282, 372, 18, 19); 
    [button addTarget:self action:@selector(flipView) forControlEvents:UIControlEventTouchDown]; 
    [self.view addSubview:button]; 
    [self.view bringSubviewToFront:button]; 
} 

但它陷在佔據button.frame空間的單元格。爲什麼不是[self.view bringSubviewToFront:button]工作?

另外,[self.tableView bringSubviewToFront:button]也不起作用。

+0

添加在哪裏ü要添加的按鈕? tableView的頂部或tableView的單元格內? – AppleDelegate

+0

頂部,而不是在一個單元格內 – Eric

+0

什麼是「但它卡在單元格佔用button.frame空間。」意思? – Eiko

回答

1

你真正想要的是一個浮動視圖,即一種觀點認爲,不動表視圖滾動時。按鈕看起來對你的單元格粘性的原因是整個表格處於滾動視圖,所以當內容移動時,按鈕也會如此。

要走的路是在表滾動時調整添加視圖的位置。只要實現委託方法:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    CGPoint buttonOrigin = CGPointMake(200,50); 
    self.button.frame = CGRectMake(buttonOrigin.x, buttonOrigin.y + self.tableView.bounds.origin.y, self.button.frame.size.width, self.button.frame.size.height); 
} 

您需要存儲您的按鈕的引用,這樣就可以在這裏訪問它,也許把按鈕的位置到伊娃也是如此。

如果您有機會獲得WWDC 2011的視頻,你會發現有絕招在會話125

-1

的你應該tableHeaderView

-(void)createFlipButton 
{ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark]; 
    button.frame = CGRectMake(10, 10, 18, 19); 
    [button addTarget:self action:@selector(flipView) forControlEvents:UIControlEventTouchDown]; 
    self.tableView.tableHeaderView = button 
    self.tableView.delegate = self; 
    [self.tableView reloadData]; 
} 



and implement the following UITableView Delegate 
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 

return 44;/// sample 
} 
+0

不能像標題視圖中一樣置頂。我想要它在我的框架上面說的地方。只是不屬於tableView – Eric

+0

嘗試將基類轉換爲UIViewController而不是UITableViewController,即在接口(.h)文件中,並在相應的XIB文件 – AppleDelegate

+0

中進行所需的更改,這正是我試圖避免的。 – Eric

0

您可以創建一個透明的UIView中添加按鈕;將您的按鈕添加到該UIView,然後將該UIView添加到您擁有的視圖層次結構中。 這應該按照你的要求工作(按鈕不是表視圖的一部分)。 如果需要任何示例代碼的幫助,請在下面添加註釋。

0

每當我想在同一視圖中使用表視圖和其他UI項目,我使用UIViewController。這個視圖控制器符合UITableViewDelegate和UITableViewDataSource。 UITableView委託和數據源方法被實現,因此這個視圖控制器也可以作爲表視圖控制器。我使用IB或故事板進行連接。但是,如果你想以編程方式創建所有東西,這也應該可以工作。

在你的情況下,如果你想讓UIButton與表視圖分開,使用上面描述的UIViewController就是其中一個選項。

-1

按鈕的UITableViewCell

[cell.contentView addSubview:Button]; 
相關問題