如何以编程方式添加文本字段和按钮?
问题描述:
我有一个tableView,我想在tableView上应用搜索工具。为此,我需要textField和搜索按钮,但我不知道如何以编程方式创建这些。所以请告诉我如何创建这两个工具。如何以编程方式添加文本字段和按钮?
thanx。
答
与下面的链接,它描述了异步滚动例如
但你可以尝试为按钮添加事件另请参阅前一篇文章 UISearchBar Sample Code
为执行并得到结果,当您键入的更多的方法,去苹果的文档和参考UISearchBarDelegate Protocol Methods
答
您有一个表代表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];
感谢