我是swift的新手。我想在表格视图中为描述字段实现json show的搜索操作。
StatusText
这是我从json
获取数据的类<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="200" Width="400"
WindowStartupLocation="CenterScreen"
ResizeMode="CanMinimize" Topmost="True">
<Window.Resources>
</Window.Resources>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="AUTO" />
<RowDefinition Height="AUTO" />
<RowDefinition Height="AUTO" />
<RowDefinition Height="AUTO" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Please wait ..." Grid.Row="1" Margin="6"/>
<TextBlock Name="tbkStatus" Grid.Row="2" Margin="6" TextWrapping="Wrap" Text="{Binding StatusText}"/>
<ProgressBar Grid.Row="3" Margin="6" Height="20"/>
<Button Grid.Row="4" HorizontalAlignment="Center" Padding="24,3" Margin="6" Content="Stop"/>
</Grid>
</Window>
这是关于表视图中描述字段的搜索过滤器功能
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell
classObject.getDataForTableView(results: results, index: indexPath.row)
//let image_url = filteredsneakernews[indexPath.row].image
// cell.sneakerImageView.image=filteredsneakernews[indexPath.row].image
cell.newsTitle.text = classObject.descriptionn
let imageURLPathString = newsurl + classObject.image
let url1 = URL(string: imageURLPathString)
print("xyz", url1)
let data = try? Data(contentsOf: url1!)
if let imageData = data {
let image = UIImage(data: imageData)
cell.sneakerImageView.image = image
}
return cell
}
如何在表视图中实现json描述字段的搜索操作。您可以从此链接下载项目。https://drive.google.com/file/d/1HGQVN3QWFSQf1BcV-3Iv-pXOoWozcP1G/view?usp=sharing
答案 0 :(得分:0)
在cellForRowAtindex中填充dict的结果数组... 过滤后,你需要检查结果数组中的数据,但是你正在检查获取数据对象..
filteredsneakernews = [classObject.descriptionn] .filter {$ 0.description.lowercased()。contains(searchController.searchBar.text!.lowercased())}而不是检查结果数组上的数据检查
results = results.filter {$ 0.description.lowercased()。contains(searchController.searchBar.text!.lowercased())}
像这样答案 1 :(得分:0)
如果您正在关注方法搜索结果,则显示tableview
class Model:NSObject {
var name1:String = ""
var size1:String = ""
var colour1:String = ""
var store1:String = ""
init (name:String, size:String, colour:String, store:String)
{
self.name1 = name
self.size1 = size
self.colour1 = colour
self.store1 = store
}
class func generatedModelArray () -> [Model] {
var modelArray = [Model]()
modelArray.append(Model(name: "Name0", size: "size0", colour: "colour0", store: "store0"))
modelArray.append(Model(name: "name1", size: "size1", colour: "colour1", store: "store1"))
modelArray.append(Model(name: "name2", size: "size2", colour: "colour2", store: "store2"))
modelArray.append(Model(name: "name3", size: "size3", colour: "colour3", store: "store3"))
return modelArray
}
// Outlet连接和声明
@IBOutlet weak var tableview: UITableView!
var list = [Model]()
var filterdArry = [Model]()
var serachController = UISearchController()
var resultController = UITableViewController()
//search
let searchController = UISearchController(searchResultsController: nil)
// viewDidLoad方法
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
serachController = UISearchController(searchResultsController: resultController)
tableview.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
resultController.tableView.delegate = self
resultController.tableView.dataSource = self
let elm = Model(name: "Name0", size: "size0", colour: "colour0", store: "store0")
list.append(elm)
let elm1 = Model(name: "name1", size: "size1", colour: "colour1", store: "store1")
list.append(elm1)
let elm2 = Model(name: "name2", size: "size2", colour: "colour2", store: "store2")
list.append(elm2)
let elm3 = Model(name: "name3", size: "size3", colour: "colour3", store: "store3")
list.append(elm3)
filterdArry = list
tableview.delegate = self
tableview.dataSource = self
tableview.register(UINib(nibName: "DetaisTableTableViewCell", bundle: nil), forCellReuseIdentifier: "detailsCell")
self.view.addSubview(tableview)
tableview.reloadData()
//搜索功能
func updateSearchResults(for searchController: UISearchController) {
filterdArry = list.filter({(mod) -> Bool in
print("mod.name1\(mod.name1)")
return mod.name1.lowercased().contains((searchController.searchBar.text)!.lowercased())
})
tableview.reloadData()
}
// Tableview方法
func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) - &gt; Int {
if tableView == resultController.tableView {
return filterdArry.count
} else {
return list.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableview.dequeueReusableCell(withIdentifier: "detailsCell") as! DetaisTableTableViewCell
if tableView == resultController.tableView {
let model = filterdArry[indexPath.row]
cell.label1.text = model.name1
cell.label2.text = model.size1
cell.label3.text = model.colour1
cell.label4.text = model.store1
} else {
let model = list[indexPath.row]
cell.label1.text = model.name1
cell.label2.text = model.size1
cell.label3.text = model.colour1
cell.label4.text = model.store1
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}