2016-04-16 84 views
2

我有一個表視圖,並希望一個部分的標題有一個右對齊的按鈕。理想情況下,我可以將該按鈕添加到UITableViewHeaderFooterView以獲取其標題字體類型,大小和顏色。在我的tableView(tableView: UITableView, viewForHeaderInSection section: Int)中,我試過創建一個UIButton並將其添加到UITableViewHeaderFooterView的子視圖和內容視圖中,但它沒有出現。有什麼建議麼?iOS添加按鈕到UITableViewHeaderFooterView右側

回答

0

試一試它的工作對我來說就像魅力,

我在willDisplayHeaderView委託方法添加的UIButton

1)添加按鈕

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
    { 
     UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [header addSubview:button]; 
     [button setFrame:CGRectMake(200, -10, 300, 44)]; 
     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:35]]; 
     [button setTitle:@"Add" forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(addbuttonclicked) forControlEvents:UIControlEventTouchUpInside]; } 

2)添加按鈕動作

-(void)addbuttonclicked 
    { 
    //Add code  
    } 

希望這對某些人有幫助。

相關問題