Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Tableview搜索栏未正确返回detailTextLabel(Xcode-Swift)_Ios_Swift_Uitableview_Firebase - Fatal编程技术网

Ios Tableview搜索栏未正确返回detailTextLabel(Xcode-Swift)

Ios Tableview搜索栏未正确返回detailTextLabel(Xcode-Swift),ios,swift,uitableview,firebase,Ios,Swift,Uitableview,Firebase,我对Xcode很陌生,所以我对搜索栏和表视图不太了解。目前为止,搜索栏工作正常,只是搜索时没有返回特定展位的正确值。请务必查看图像,以便于参考。非常感谢你的帮助。谢谢大家! import UIKit import FirebaseDatabase var ref: DatabaseReference? var databaseHandle: DatabaseHandle? var postData = [String]() var postData2 = [String]() var curr

我对Xcode很陌生,所以我对搜索栏和表视图不太了解。目前为止,搜索栏工作正常,只是搜索时没有返回特定展位的正确值。请务必查看图像,以便于参考。非常感谢你的帮助。谢谢大家!

import UIKit
import FirebaseDatabase

var ref: DatabaseReference?
var databaseHandle: DatabaseHandle?
var postData = [String]()
var postData2 = [String]()
var currentpostDataArray = [String]()
var tableDataArray = [tableData]()

class TableViewController: UITableViewController, UISearchBarDelegate {


    @IBOutlet var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpSearchBar()


        ref = Database.database().reference() //set the firebase reference

        // Retrieve the post and listen for changes
        databaseHandle = ref?.child("Posts2").observe(.value, with: { (snapshot) in
            // Code to execute when a child is added under "Posts"
            postData.removeAll()
            postData2.removeAll()
            tableDataArray.removeAll()

            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let key = snap.key
                let value = String(describing: snap.value!)
                let rating = (value as NSString).integerValue

                postData.append(key)
                postData2.append(value)

                tableDataArray.append(tableData(boothName: key, boothRating: rating))
                currentpostDataArray = postData
            }

            self.tableView.reloadData()


        })

    }


    private func setUpSearchBar() {
        searchBar.delegate = self
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return currentpostDataArray.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = currentpostDataArray[indexPath.row]
        cell.detailTextLabel?.text = postData2[indexPath.row] + " ♥"
        cell.detailTextLabel?.textColor = UIColor.red;
        return cell
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        currentpostDataArray = postData.filter({ (postdata) -> Bool in
            switch searchBar.selectedScopeButtonIndex {
            case 0:
                if searchText.isEmpty { return true }
                return postdata.lowercased().contains(searchText.lowercased())
            case 1:
                if searchText.isEmpty { return true }
                return postdata.lowercased().contains(searchText.lowercased())
            default:
                return false
            }
        })
        tableView.reloadData()
    }


        func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
            switch selectedScope {
            case 0:
                currentpostDataArray.removeAll()
                postData2.removeAll()
                for data in tableDataArray {
                    currentpostDataArray.append(data.boothName)

                    let value = String(describing: data.boothRating)
                    postData2.append(value)
                }
                self.tableView.reloadData()

            case 1:
                currentpostDataArray.removeAll()
                postData2.removeAll()
                let sortedTableData = tableDataArray.sorted(by: { $0.boothRating > $1.boothRating })
                for data in sortedTableData {
                    currentpostDataArray.append(data.boothName)

                    let value = String(describing: data.boothRating)
                    postData2.append(value)
                }
                self.tableView.reloadData()

            default:
                break

            }
            tableView.reloadData()
        }

}

class tableData {
    var boothName: String
    var boothRating: Int
    init(boothName: String, boothRating: Int) {
        self.boothName = boothName
        self.boothRating = boothRating
    }
}

之所以发生这种情况,是因为在您的代码中postData包含密钥。

 for child in snapshot.children {
            let snap = child as! DataSnapshot
            let key = snap.key
            let value = String(describing: snap.value!)
            let rating = (value as NSString).integerValue

            ***postData.append(key)***

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
您将返回post date内容,该内容只包含键,不包含值。这里您没有更新postData2

So

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel?.text = currentpostDataArray[indexPath.row]
    cell.detailTextLabel?.text = postData2[indexPath.row] + " ♥" 
    cell.detailTextLabel?.textColor = UIColor.red;
    return cell
}
cell.detailTextLabel?.text采用的是postData2的旧值。因为在postData2中,第一个索引值是38

我想你明白了

解决方案:

不要创建两个数组来处理数据。您可以创建一个字典数组来处理数据。在您的例子中,它是currentpostDataArray和postData2

我将使用单个字典数组修改代码

override func viewDidLoad() {
    super.viewDidLoad()

    setUpSearchBar()


    ref = Database.database().reference() //set the firebase reference

    // Retrieve the post and listen for changes
    databaseHandle = ref?.child("Posts2").observe(.value, with: { (snapshot) in
        // Code to execute when a child is added under "Posts"
        postData.removeAll()
        postData2.removeAll()
        tableDataArray.removeAll()

        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let key = snap.key
            let value = String(describing: snap.value!)
            let rating = (value as NSString).integerValue

            postData.append(key)
            postData2.append(value)

            tableDataArray.append(tableData(boothName: key, boothRating: rating))
            currentpostDataArray = tableDataArray
        }

        self.tableView.reloadData()


    })

}

 // cellForRowAtIndexpath

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel?.text = currentpostDataArray[indexPath.row]. boothName
    cell.detailTextLabel?.text = currentpostDataArray[indexPath.row]. boothRating + " ♥"
    cell.detailTextLabel?.textColor = UIColor.red;
    return cell
}

//searchBar delegate

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    currentpostDataArray = tableDataArray
    for item in tableDataArray {
        let str = item.boothName
        if str.lowercased().range(of: searchText) != nil {
            currentpostDataArray.append(item)
        }
    }


    tableView.reloadData()
}

你的期望是什么。添加一些你期望的图片。如果你仔细看图片1,3号展位应该有6颗红心,但返回38颗属于1号展位的红心。我需要搜索栏把合适的心返回到boothsk找到你的问题了好的我找到了更新postData2的合适代码是什么?我对此很陌生。是的,我的boothRating是Int格式的,detailTextLabel只接受字符串值:/所以有什么问题。将其转换为字符串:cell.detailTextLabel?.text=String(currentpostDataArray[indexath.row].boothRating)+”♥"