2011-06-21 99 views
0

我已經在UITableViewCell中成功實現了UITextField。就像這樣:UITableViewCell中的UITextField - UIButton

enter image description here

我在上面使用了代碼:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

現在我想在這兩個UITableViewCells下放置一個UIButton。我可以編碼一個UIButton嗎?我怎樣才能做到這一點,在哪裏?

謝謝。

新的按鈕位置

enter image description here

Interface Builder中

enter image description here

+0

你想索引2的按鈕權利? – iAmitWagh

+0

嘿,超級棒!你在哪裏需要UIBUTTON? – Splendid

回答

1

您可以在同樣的方法也做了,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexPath.row==2) 
    { 
    //Create a UIButton 
    //Assign target 
    //Add the button as a subview to cell.contentView 
    } 
else 
    { 
    //Add the textFields 
    } 
} 

創建一個按鈕代碼,

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    myButton.frame = CGRectMake(20, 20, 200, 44); // position in the cell and set the size of the button 
    [myButton setTitle:@"Login" forState:UIControlStateNormal]; 
    // add targets and actions 
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    // add to a view 
    [cell.contentView addSubview:myButton]; 
+0

@KingofBliss:我已經完成了上述操作,但是如何才能使我的UITableviewCell的第三行消失,並且按鈕居中,而不是主要位於左側?請參閱上面的新屏幕截圖。謝謝。 –

+0

更改按鈕位置爲60,3,200,38 – KingofBliss

+0

@KingofBliss:酷!有沒有辦法讓tableviewCell上的第三個單元消失,但UIButton留下來?或者我只需要用UIButton覆蓋第三個單元格?謝謝。 –

0

你可能不得不做出一個UIViewController表視圖的控制器。這樣,你可以添加一個UIButton到視圖以及表視圖。

只需使UIViewController成爲UITableViewDelegate和UITableViewDataSource,就可以在UIViewController中創建一個UITableView,並將UITableViewController中的代碼複製到UIVIewController中。然後在它下面添加一個UIButton。

這就是我過去所做的。我只使用UITableViewController,如果我只想爲列表等事物展開一個表視圖。

1

您可以使用此按鈕添加到的tableView的頁腳視圖

tableView.tableFooterView = [[[UIButton alloc] initWith:] autorelease]; 

或者您可以將按鈕添加到節頁腳視圖。 使用方法

viewForFooterInSection