我想在tableView中使用searchbar.我有一个带有人的NSArray(NSObject).在cellForRow中,我正在使用此代码:
Person *person = [[self.sectionedPersonName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = person.fullName;
Run Code Online (Sandbox Code Playgroud)
并为searchBar这段代码:
- (void)searchForText:(NSString *)searchText {
if (searchText.length > 0) {
searchBar = YES;
} else {
searchBar = NO;
}
NSLog(@"%@", self.sectionedPersonName);
NSPredicate *predicate; = [NSPredicate predicateWithFormat:@"SELF.fullName beginswith[c] %@", searchText];
self.searchResults = [self.sectionedPersonName filteredArrayUsingPredicate:predicate];
}
Run Code Online (Sandbox Code Playgroud)
self.sectionedPersonName
包含这个:
(
(
"<Person: 0x7fc0f6012650>"
),
(
),
(
),
(
"<Person: 0x7fc0f600f8a0>",
"<Person: 0x7fc0f60132e0>"
),
(
),
(
),
(
),
(
"<Person: 0x7fc0f6012d80>"
),
(
),
(
"<Person: 0x7fc0f6012b30>"
),
(
"<Person: …
Run Code Online (Sandbox Code Playgroud) 我正在设计一个搜索栏。我需要搜索栏看起来像图像中的一个。尝试了几种方法,但没有改变。非常感谢帮助。
[已编辑] 使用 Richard 的代码后查看图像,它看起来像这样。我希望灰色是清晰的。
有没有一种方法UISearchController
可以让我使用故事板UISearchBar
?我有一个带有导航栏的故事板 UI,其中包含我的搜索栏。如果我能保持这种方式就好了,因为以编程方式设置布局真的很不方便。我知道旧的UISearchDisplayController
可以故事板。有没有办法用 UISearchController 做类似的事情?
这是我在导航栏中使用 UISearchBar 的视图:
我希望它在我的视图加载时自动聚焦。我根据这个问题尝试了几种方法。但没有一个工作。我必须单击搜索栏才能使其集中。
这是我的代码之一:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.searchController.isActive = true
DispatchQueue.main.async { [unowned self] in
self.searchController.searchBar.becomeFirstResponder()
}
}
Run Code Online (Sandbox Code Playgroud)
有人提到 searchController 应该在 becomeFirstResponder 之后处于活动状态。我试过这个:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async { [unowned self] in
self.searchController.searchBar.becomeFirstResponder()
self.searchController.isActive = true
}
}
Run Code Online (Sandbox Code Playgroud)
这次键盘确实出来了。但是我无法在搜索栏中输入任何内容。
任何的想法?谢谢。
我应该如何处理这个问题?我不知道如何将搜索栏与 UICollectionView 正确集成。请帮助我理解这个问题。
PS 我的语言是 Swift
[1]
[2]
导入 UIKit类 MainViewController: UICollectionViewController {
Run Code Online (Sandbox Code Playgroud)override func viewDidLoad() { super.viewDidLoad() } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 14 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrendingGifCell", for: indexPath) return cell }
}
我实现了标准的 SearchView。但是当搜索处于活动状态时,SearchView 右侧有一个奇怪的空间(见图)。源代码在这里。如何将关闭按钮对齐到右侧?我在 Android 7 上测试过。我必须在这里写更多的文字,但没有理由。
菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.android.nigeriaexams.ui.SettingsFragment">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:layout_width="wrap_content" />
</menu>
Run Code Online (Sandbox Code Playgroud)
活动:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
//*** setOnQueryTextFocusChangeListener ***
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String searchQuery) {
myAppAdapter.filter(searchQuery.toString().trim());
listView.invalidate();
return true; …
Run Code Online (Sandbox Code Playgroud) 我是 Swift 新手,想知道是否有可能将 UIsearchcontroller 放置在 navgationtitle 中,并通过位于 navigationRightItem 的按钮隐藏和取消隐藏 uisearchcontroller。我见过很多将 UISearchBar 从对象库放置到 navigationTitle 的示例,但没有 uisearchcontroller 的示例。有些人甚至说对象库中的 UISearch bar 已从 ios10 Swift 中弃用。请澄清弃用以及为什么我应该避免使用对象库中的 UISearchBar。哪一种是用于 firebase tableview 搜索的最佳实践。如果您能提供代码示例,我将很高兴。先感谢您。
我在导航控制器上以编程方式更改我的搜索栏设计时遇到问题。我只想使搜索栏曲线如下图所示:
我在堆栈溢出中尝试了很多示例,但我仍然无法改变形状。这是现在的样子:
这是源代码,关于已经完成的内容:
//MARK for searchBar UI
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.placeholder = " Search for programs..."
searchController.searchBar.sizeToFit()
searchController.searchBar.isTranslucent = false
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.delegate = self
navigationItem.titleView = searchController.searchBar
self.searchController.searchBar.layer.cornerRadius = 20.0
self.searchController.searchBar.clipsToBounds = true
// SearchBar text
let textFieldInsideUISearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideUISearchBar?.borderStyle = .roundedRect
textFieldInsideUISearchBar?.font = textFieldInsideUISearchBar?.font?.withSize(14)
Run Code Online (Sandbox Code Playgroud)
现在,我真的不知道如何更改它,提前致谢。
uisearchbar uinavigationitem swift uisearchcontroller programmatically
我使用 RxSwift 在我的 tableview 中显示人员列表,我的 tableview 有两个部分,第一个是旧搜索,第二个是所有人员。现在我不知道当用户在 UISearchBar 的文本字段上输入名称时我应该如何过滤人员。
这是我的人模型:
struct PersonModel {
let name: String
let family:String
let isHistory:Bool
}
Run Code Online (Sandbox Code Playgroud)
这是我的 ContactsViewModel
struct SectionOfPersons {
var header: String
var items: [Item]
}
extension SectionOfPersons: SectionModelType {
typealias Item = PersonModel
init(original: SectionOfPersons, items: [SectionOfPersons.Item]) {
self = original
self.items = items
}
}
class ContactsViewModel {
let items = PublishSubject<[SectionOfPersons]>()
func fetchData(){
var subItems : [SectionOfPersons] = []
subItems.append( SectionOfPersons(header: "History", items: [
SectionOfPersons.Item(name:"Michelle", family:"Obama", isHistory:true ),
SectionOfPersons.Item(name:"Joanna", family:"Gaines", …
Run Code Online (Sandbox Code Playgroud) 我对 aUISearchBar/UISearchBarDelegate
和 a之间的区别感到困惑UISearchBarController
。
现在,这就是我为实现搜索栏所做的工作:
class SearchViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
}
extension SearchViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
if !searchBar.text!.isEmpty {
performSegue(withIdentifier: "Search", sender: searchBar.text!)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在网上看到有人在做这样的事情:
let searchController = UISearchController(searchResultsController: nil)
Run Code Online (Sandbox Code Playgroud)
这和我正在做的事情有什么区别?
uisearchbar ×10
ios ×6
swift ×5
objective-c ×2
xcode ×2
android ×1
nsarray ×1
nspredicate ×1
rx-swift ×1
search ×1
swift3 ×1
uikit ×1
uistoryboard ×1
uitableview ×1