如何以编程方式添加文本字段和按钮?

问题描述:

我有一个tableView,我想在tableView上应用搜索工具。为此,我需要textField和搜索按钮,但我不知道如何以编程方式创建这些。所以请告诉我如何创建这两个工具。如何以编程方式添加文本字段和按钮?

thanx。

与下面的链接,它描述了异步滚动例如

http://blog.patrickcrosby.com/2010/04/27/iphone-ipad-uisearchbar-uisearchdisplaycontroller-asynchronous-example.html

但你可以尝试为按钮添加事件另请参阅前一篇文章 UISearchBar Sample Code

为执行并得到结果,当您键入的更多的方法,去苹果的文档和参考UISearchBarDelegate Protocol Methods

以下是UITextFieldUIButton的文档。您也许会发现UISearchBar有用,它包含文本字段和按钮。

将您的意见添加到表视图的标题视图。

您有一个表代表tableview:cellForRowAtIndexPath。在这个委托中添加下面的代码。

//Suppose you want to add textfield and button at the first row 
if(indexPath.Row == 0) 
{ 
     UITextField *txtSearch = [UITextField alloc] initWithFrame:CGRectMake(0,0,150,35)]; 
     UIButton *btnSearch = [UIButton buttonWithType:UIButtoTypeCustom]; 
     btnSearch.frame = CGRectMake(160,0,50,35); 

     [[cell contentView] addSubView:txtSearch]; 
     [[cell contentView] addSubView:btnSearch]; 
} 

,你也可以像

[btnSearch addTarget:self action:@selector(eventName) forControlEvents:UIControlEventTouchUpInside]; 

感谢