2011-07-14 28 views
2

我創建一個自定義按鈕,並把它放在我的桌子的頁腳視圖但按鈕從右上角將會出路放置一個按鈕。表頁腳視圖

什麼是錯在這裏!?!

這裏是我的代碼,並輸出圖像:

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    [aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled]; 
    [aButton setBackgroundImage:[[UIImage imageNamed:@"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal]; 
    [aButton setTitle:@"Click me" forState:UIControlStateNormal]; 
    [aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]]; 
    [aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)]; 
    [self.tableView setTableFooterView:aButton]; 

enter image description here

+0

不應幀是:(0.0,0.0,300.0,44.0)? – 2011-07-14 19:30:59

+0

@Vince:這會讓按鈕畫在左上角,我相信Abhinav並不需要。 – EmilioPelaez

+0

爲什麼不嘗試像這樣'CGRectMake(10.0,15.0,self.tableView.frame.size.width - 20,44.0)'設置框架? – EmilioPelaez

回答

14

我加入了UIView的按鈕,然後tableFooterView設置爲此UIView的對象和它的工作添加它作爲一個子視圖。我們不能直接把UIButton作爲tableFooterView。

+1

究竟.......! – Legolas

+1

您可以,但表格視圖將autoResizingMask設置爲全部四個角。你應該改變它來讓它正常工作。 – Mugunth

-1

嘗試做這樣的。設置IBOutlet UIView *footerView;併合成它。使用方法 -

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 

if(footerView == nil) { 
    //allocate the view if it doesn't exist yet 
    footerView = [[UIView alloc] init]; 


    //create the button 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    [button setFrame:CGRectMake(10, 3, 300, 44)]; 

    //set title, font size and font color 
    [button setTitle:@"Click Me" forState:UIControlStateNormal]; 
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]]; 

    //set action of the button 
    [button addTarget:self action:@selector(clickPressed:) 
    forControlEvents:UIControlEventTouchUpInside]; 

    //add the button to the view 
    [footerView addSubview:button]; 
} 

//return the view for the footer 
return footerView; 
} 
+2

來自UITableView.h:'@property(nonatomic,retain)UIView * tableFooterView; //以下內容的附件視圖。默認爲零。不要與節頁腳混淆 ' – EmilioPelaez

-3

一個重要的除了萊格拉斯精答案:你需要實現的tableView委託方法:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { 
    foot 50; 
} 

這是我逼瘋,因爲按鈕(或者,在我的情況,按鈕)不迴應觸摸事件。添加委託方法使其更好。 :-)

+2

由於tableFooterView!= footerInSection和Abhinav要求第一個,Legolas答案在這種情況下不好。第一個是整個表格,第二個僅用於部分(你可以有很多)。檢查EmilioPelaez的評論。 – Vive