स्विफ्ट आईओएस एप्लिकेशन में JSON डिकोडर का उपयोग करके नाम या पूंजी JSON को फ़िल्टर करने के लिए UISearchBar को कैसे कार्यान्वित करें। मैं JSON डेटा से नाम का उपयोग करके UISearchBar और खोज परिणामों या फ़िल्टर परिणामों को कार्यान्वित करना चाहता हूं।
import UIKit
संरचना बनाई गई
struct jsonstruct:Decodable
{
let name:String
let capital:String
}
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate, UISearchBarDelegate, UISearchControllerDelegate, UISearchDisplayDelegate {
TableView और SearchBar के लिए आउटलेट बनाना
@IBOutlet var tableview: UITableView!
@IBOutlet var searchBar: UISearchBar!
JSON घोषित करना
var arrdata = [jsonstruct]()
डेटा प्राप्त करने के लिए कार्य
func getdata()
{
let url = URL(string: "https://restcountries.eu/rest/v2/all")
URLSession.shared.dataTask(with: url!)
{
(data, response, error) in
do
{
if error == nil
{
self.arrdata = try
JSONDecoder().decode([jsonstruct].self, from: data!)
for mainarr in self.arrdata
{
print(mainarr.name,":",mainarr.capital as Any)
DispatchQueue.main.async
{
self.tableview.reloadData()
}
}
}
}
catch
{
print(error.localizedDescription)
}
}.resume()
}
तालिका दृश्य
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.arrdata.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
cell.label1.text = "Name: \(arrdata[indexPath.row].name)"
cell.label2.text = "Capital: \(arrdata[indexPath.row].capital)"
return cell
}
ओवरराइडिंग फंक्शन
override func viewDidLoad()
{
getdata()
}
3 जवाब
टेक्स्टफिल्ड को आईबीएक्शन के आईबीएक्शन के साथ नीचे की तरह बनाएं और एक सरणी बनाएं जहां आप डेटा फ़िल्टर कर सकें।
@IBAction func tfSearch(_ sender: UITextField) {
let filteredArray = yourArr.filter { $0.contains(sender.text) }
}
आपको डेटा के दो ऑब्जेक्ट बनाने होंगे, एक मूल डेटा और दूसरा फ़िल्टर किया गया डेटा।
var filteredArrData = [jsonstruct]()
var arrdata = [jsonstruct]()
आपके getData फ़ंक्शंस से:
do {
self.arrdata = try JSONDecoder().decode([jsonstruct].self, from: data!)
self.filteredArrData = self.arrdata
}
फिर अपनी तालिका में प्रतिनिधि और डेटा स्रोत देखें:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.filteredArrData.count
}
Func तालिका दृश्य (_ तालिका दृश्य: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
cell.label1.text = "Name: \(filteredArrData[indexPath.row].name)"
cell.label2.text = "Capital: \(filteredArrData[indexPath.row].capital)"
return cell
}
इस तरह से फ़िल्टर फ़ंक्शन करें:
func applyFilters(textSearched: String) {
filteredArrData = arrdata.filter({ item -> Bool in
return item.name.lowercased().hasPrefix(textSearched.lowercased())
})
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
फिर अपनी स्ट्रिंग को इस फ़ंक्शन में पास करें और सब कुछ ठीक काम करेगा।
मान लें कि आप अपने सभी डेटा को कैशिंग नहीं कर रहे हैं और फ़िल्टरिंग एक एपीआई के माध्यम से लाइव की जाती है। आपको ऑब्जेक्ट या व्यू कंट्रोलर को सर्च बार (UISearchBarDelegate) के प्रतिनिधि के रूप में सेट करने की आवश्यकता होगी। फिर अपनी API क्वेरी के लिए टेक्स्ट के रूप में searchText का उपयोग करें।
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
//call throttle that will call urlsession
}
चूंकि एक बार में एक अक्षर टाइप किया जाता है, इसलिए हम हर बार एपीआई को कॉल नहीं करते हैं। आपको वर्ण खोज द्वारा वर्ण भेजने के बजाय कम API कॉल करने के लिए थ्रॉटलर का उपयोग करने की आवश्यकता हो सकती है। आपको थ्रॉटलिंग के बारे में यह ट्यूटोरियल मददगार लग सकता है: सरल थ्रॉटलिंग इन स्विफ्ट।
अधिकांश आरईएसटी एपीआई में एक फ़िल्टर सुविधा होनी चाहिए और आप आसानी से टाइप किए गए नाम या पूंजी को जोड़ सकते हैं।
https://restcountries.eu/rest/v2/name/append name here
https://restcountries.eu/rest/v2/capital/append capital here
यह परिणाम लाने के लिए नेटवर्किंग कोड का एक उदाहरण है। तालिकादृश्य को पुनः लोड करने के लिए मुख्य कतार पर सुरक्षित रूप से किसी अन्य विधि को कॉल करने के लिए परिणामों का उपयोग करें।
if let url = URL(string: "https://restcountries.eu/rest/v2/country?q=name") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let results = try JSONDecoder().decode(YourCustomDecodeStruct.self, from: data)
//safely your data source and reload the tableview
} catch let error {
print(error)
}
}
}.resume()
}
संबंधित सवाल
नए सवाल
ios
iOS, Apple iPhone, iPod टच और iPad पर चलने वाला मोबाइल ऑपरेटिंग सिस्टम है। IOS प्लेटफॉर्म पर प्रोग्रामिंग से संबंधित प्रश्नों के लिए इस टैग [ios] का उपयोग करें। उन प्रोग्रामिंग भाषाओं के लिए विशिष्ट मुद्दों के लिए संबंधित टैग [उद्देश्य-सी] और [स्विफ्ट] का उपयोग करें।