gpt4 book ai didi

ios - 过滤数据数组以在 UITableView 中使用

转载 作者:行者123 更新时间:2023-11-29 01:38:28 24 4
gpt4 key购买 nike

我正在尝试根据我查询的对象(作为字符串)过滤我创建的数组。它们显示得很好,现在我只想过滤并重新制作数组,以便过滤出我需要的信息。我不确定为什么我在 Xcode 中收到“调用‘过滤器’的结果未使用”的消息。我环顾四周,但我想不出这个。

import UIKit

class RegionStoreTableViewController: UITableViewController, UISearchBarDelegate {

var selectedRegionStore : String? = nil
var selectedRegionStoreIndex : Int? = nil
var dataArray = [String]()
var filteredArray = [String]()
var employeeType : String? = nil
var searchText = ""

@IBOutlet weak var regionStoreSearchBar: UISearchBar!

override func viewDidLoad() {
super.viewDidLoad()

let prefs = NSUserDefaults.standardUserDefaults()

if prefs.valueForKey("EmployeeType") != nil {

employeeType = prefs.valueForKey("EmployeeType") as! String

// Employee Type

// Retail
if employeeType == "Retail" {

self.navigationItem.title = "Store Selector"

let query = PFQuery(className: "Stores")
query.orderByAscending("rStoreNumber")
query.findObjectsInBackgroundWithBlock({ (store: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
for store in store! {
let theStore = store["storeName"] as! String
let storeNumber = store["rStoreNumber"] as! String
let storeString = storeNumber + " - " + theStore
print(theStore)
self.dataArray.append(storeString)
self.tableView.reloadData()
}
}
})



}

if employeeType == "Corporate" {
let query = PFQuery(className: "Regions")
query.orderByAscending("regionName")
query.findObjectsInBackgroundWithBlock { (region: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
for region in region! {
let theRegion = region["regionName"] as! String
print(theRegion)
self.dataArray.append(theRegion)
self.tableView.reloadData()
}
} else {
print(error)
}
}
}
}
}




override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(dataArray.count)
return dataArray.count

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("RegionStoreCell", forIndexPath: indexPath)

if searchText.isEmpty {
cell.textLabel?.text = dataArray[indexPath.row]
}

if searchText != "" {
dataArray.filter() {nil != $0.containsString(searchText) }
}

if indexPath.row == selectedRegionStoreIndex {
cell.accessoryType = .Checkmark
} else {
cell.accessoryType = .None
}
return cell as UITableViewCell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)

if let index = selectedRegionStoreIndex {
let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0))
cell?.accessoryType = .None
}

selectedRegionStoreIndex = indexPath.row
selectedRegionStore = dataArray[indexPath.row]

let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SaveSelectedRegionStore" {
let cell = sender as! UITableViewCell
let indexPath = tableView.indexPathForCell(cell)
selectedRegionStoreIndex = indexPath?.row
if let index = selectedRegionStoreIndex {
selectedRegionStore = dataArray[index]
}
}
}

// MARK: Search Bar
// delegate in story board
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = false
}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
// add minimum length of search
searchText = searchBar.text!
self.tableView.reloadData()
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
// clear out search box
searchBar.text = nil
// clear out search variable
searchText = ""
// reload the table
self.tableView.reloadData()
// hide keyboard
searchBar.resignFirstResponder()
}


}

有什么建议吗?

最佳答案

我认为您需要将过滤后的数组存储到另一个数组中。

let filterArray = dataArray.filter() {nil != $0.containsString(searchText) 

关于ios - 过滤数据数组以在 UITableView 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32710154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com