【问题标题】:Filter an Array of data for use in a UITableView过滤数据数组以在 UITableView 中使用
【发布时间】:2015-12-19 00:49:53
【问题描述】:

我正在尝试过滤基于我查询的对象(作为字符串)创建的数组。它们显示得很好,现在我只想过滤并重新制作数组,以便过滤掉我需要的信息。我不确定为什么我在 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()
}


}

有什么建议吗?

【问题讨论】:

  • TL;博士。将其切成相关的部分。

标签: ios arrays swift uitableview filtering


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2018-11-06
    • 1970-01-01
    • 2022-10-15
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多