I have got a problem with UI SearchBar in swift. My problem is I want to use searchbar to find the word in Table View Cell. The word is not an English word.
Here is my code.
if ((searchText.contains("ၵ"))) {
filteredArray = homeDict.filter { $0._meaning.localizedCaseInsensitiveContains(searchText) }
}else{
filteredArray = homeDict.filter { $0._word.localizedCaseInsensitiveContains(searchText) }
}
if(filteredArray.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
That is work for only one word of "ၵ". I want to search, if the word in Searchbar the same as any word in Table View Cell, display result in cell row.
I believe this function uses the current Locale, not necessarily the locale that the letters might be in. Try testing with the device in the locale of the language the strings are in to check.
You could probably make your own contains with func lowercased(with locale: Locale?) -> String
You would need to know the locale the user intended the search to be in. You could also use this function to test what the lowercase version of a string is in various locales (to test the theory above).
Related
sorry if the title not describe the problem well
i have 4 button with 4 letters ["g","o","o","d"]
it is a game about
if the player can guess the word ,
which is in this case the word is : "good"
i have a hint button that do this :
*give one letter from the four
*hide the button with the given letter
it works very well
but the problem is :
the word "good"
has two "o" letter
so the hint button now well delete two button with the letter "O"
here is what i did so far :
// this method will add a letter from the right word "qlap" to the answer textfield and it work perfectly :
let text_ans = ans.text?.count
ans.text! += qlab![text_ans!+0]
// the four letters
let buttin = [Letter1,Letter2,Letter3,Letter4]
// check if hint letter above == button title letter
for title in buttin {
if qlab![text_ans!+0] == title!.title(for: .normal) {
let tag = title!.tag
if title?.tag == tag {
// how can i just hide for once if there is a duplicate
title?.isHidden = true
}
}
}
and here is a gif to make it more clear :
i tried do it with the button tags rather than title
but at the end i went to the same result
You could use the first(where:) (documented here) function to find the first button with a title that matches what you are looking for and where isHidden == false.
So something along the lines of:
if let hintButton = buttin.first(where: { $0.title(for: .normal) == qlab![text_ans!+0] && !$0.isHidden }) {
// now you have the first - not hidden - button matching what you are looking for, time to hide it
hintButton.isHidden = true
}
note: I haven't checked the above in a compiler so there might be syntax errors which I'm sure the compiler will inform you about in it's friendly manner.
I have a register View controller. I have text field for the phone number, and another text field for the country number.
i need the country code text field to be on the left and behind it, we should have the phone number.
this is working when the app is on English lang, but when the user changes the language to Arabic, we will get the phone number text field on the left not the country code on the left.
so, i tried to re-arrange the stack view. and it worked, but the size of the stack just changed!
let language = getObjectFromUserDefault(forKey: userDefaultKeys.getLanguageKey) as? String
if KLanguageCode.Arabic.rawValue == language
{
self.mobiStack.removeArrangedSubview(self.mobiStack.arrangedSubviews.first!)
self.mobiStack.removeArrangedSubview(self.mobiStack.arrangedSubviews.first!)
self.mobiStack.setNeedsLayout()
self.mobiStack.layoutIfNeeded()
self.view.layoutIfNeeded()
self.mobiStack.insertSubview(self.txtMobile, at: 0)
self.mobiStack.insertSubview(self.countryCode, at: 1)
self.mobiStack.setNeedsLayout()
self.view.layoutIfNeeded()
}
You can simplify your code like this:
// properly unwrap optional
guard let v = self.mobiStack.arrangedSubviews.last else {
return
}
self.mobiStack.insertArrangedSubview(v, at: 0)
Notes:
when adding arranged subviews, you do NOT need to remove them first.
the above code will swap the two views.
You may want to explicitly set the order (instead of just swapping them)... So, you might use a function like this:
func orderFields(_ rtl: Bool) -> Void {
if rtl {
self.mobiStack.addArrangedSubview(self.txtMobile)
self.mobiStack.addArrangedSubview(self.countryCode)
} else {
self.mobiStack.addArrangedSubview(self.countryCode)
self.mobiStack.addArrangedSubview(self.txtMobile)
}
}
You can call that when you setup your views --
The fields will be ordered as instructed... adding them if they are not already arranged subviews, or re-ordering them if they are.
Also, unless you have something else going on that you have not shown us, there is no need to be calling:
self.mobiStack.setNeedsLayout()
self.view.layoutIfNeeded()
I have used app.staticTexts["String"].tap() to tap on a button containing that string which is working completely fine.
But the problem here is that i want to print all the static texts which are present on that page, how can i do it in XCUITest ?
My aim here is to iterate through all the static texts present on the page and then add an if condition on my expected text.
You can use something like that:
for staticText in app.staticTexts.allElementsBoundByIndex {
if staticText.label == "test" {
}
}
//Returns all the labels,buttons, textfield,images in the page with its name.
//Just change the element name in loop according to the need.
for staticText in app.staticTexts.allElementsBoundByIndex{
if staticText != nil{
print(staticText.label)
}else{
print("No static text found")
}
}
I suppose the text is present in the identifier attribute of the staticText element. You can try the following
for i in 0..<app.staticTexts.count {
let text = app.staticTexts.element(boundBy: i).identifier
print(text)
if text == "Your String" {
// Your code
}
}
I'm a beginner and learning through doing. I made a list of universities on a tableview and I had a searchbar to filter the list. It is still working perfectly with English letters, but not for the Korean letters.
var filteredUniversities = University.generateUniversities()
override func viewDidLoad() {
super.viewDidLoad()
self.searchBarSetup()
}
func searchBarSetup() {
let searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: (UIScreen.main.bounds.width), height: 70))
searchBar.delegate = self
self.tableView.tableHeaderView = searchBar
}
//MARK: Search Bar Delegate
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
//called when text changes (or clears)
if searchText.isEmpty {
filteredUniversities = University.generateUniversities()
self.tableView.reloadData()
}else {
filterTableView(text: searchText)
}
}
func filterTableView(text:String) {
filteredUniversities = filteredUniversities.filter({ (mod) -> Bool in
return (mod.name.contains(text))
})
self.tableView.reloadData()
}
Does anyone know how to make the .filter recognize Korean letters? My universities are listed as follows. (there are many more, but they all follow the same format)
class func generateUniversities() -> [University]{
var universities: [University] = []
universities.append(University(name: "파고다 어학원", location: "부산", photo: UniversityPhotoDictionary["PAG"]!))
universities.append(University(name: "부산대학교", location: "부산", photo: UniversityPhotoDictionary["PNU"]!))
universities.append(University(name: "동아대학교", location: "부산", photo: UniversityPhotoDictionary["DAU"]!))
return universities.sorted(by: {$0.name < $1.name} )
}
Strangely, the sorted() method can handle the Korean characters, but it seems like the filter method is breaking down. (only for Korean characters, the English ones are fine)
So if I type in 파고다, it filters everything out and there are no universities to choose from.
Thanks a lot!
I assume you're working on a Korean keyboard, and when you type 파, I assume you first press ㅍ and then press ㅏ. (Forgive me if I'm wrong about the specifics of how you're typing here; my Korean is horrible, but I suspect you're doing something similar to this.)
Pressing ㅍ calls your filter routine (since it changes the text field). None of the strings contain ㅍ, so it removes everything. You then press ㅏ and it composes the character 파 and filters again, but everything's already gone.
You don't want to keep reassigning filteredUniversities. You want to filter every time on the full list. That way, even though you get nothing for ㅍ, you'll get a list for 파.
If you copy 파고다 and then paste it into this field (rather than typing it), I bet it works, since the filtering routine will only be called once.
I have a large array of size around 100000 entries. I am displaying it when user search in UISearchBar and it display filtered data on tableview. However the search bar is too slow while doing dynamic search. i.e I am filtering the table everytime the user puts in a character on search bar. As a result it gives entered text on searchBar as very late response. I just need to wait for it to appear on searchBar.
This is what I am trying to filter my array.
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filtered = data.filter({ (text) -> Bool in
let tmp: NSString = text
let range = tmp.rangeOfString(searchText, options: [NSStringCompareOptions.CaseInsensitiveSearch ,
NSStringCompareOptions.AnchoredSearch])
return range.location != NSNotFound
})
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
}
Have you ever try query with Realm.io?
I Knew that faster than any DB manager in iOS now.
Realm.io
Just make sure that you are not blocking the main_queue. Maybe you are calling sqlite functions from main queue. If so, try using FMDatabaseQueue and call the methods from background queue and update the tableview from main queue. This will handle everything for you.