2011-06-21 101 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