选择tableview中的行后,tableview将恢复为其初始状态

时间:2018-08-04 18:15:18

标签: ios swift

当我在tableView中搜索用户并选择所需的用户时,tableView将显示还原到初始用户。这是控制器的代码。预先感谢您的帮助!

import UIKit
import Firebase

class NewMessageController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {

private func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    filterSearchController(searchBar: searchBar)
}

var messagesController: MessagesController?
let cellID = "cellId"
var users = [User]()
private var filtered = [User]()

private let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.backgroundColor = UIColor(r: 243, g: 237, b: 237)
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelHandle))
    tableView.register(UserCell.self, forCellReuseIdentifier: cellID)
    fetchUsers()
    navigationController?.navigationBar.shadowImage = UIImage()
    searchController.searchBar.searchBarStyle = UISearchBarStyle.prominent
    searchController.searchBar.placeholder = "Enter the name..."
    searchController.searchBar.sizeToFit()
    searchController.searchBar.isTranslucent = true
    searchController.searchBar.backgroundImage = UIImage()
    //navigationItem.titleView = searchController.searchBar
    //searchController.searchBar.returnKeyType = UIReturnKeyType.done
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    searchController.searchResultsUpdater = self
    tableView.tableHeaderView = searchController.searchBar
    searchController.obscuresBackgroundDuringPresentation = false
    tableView.reloadData()
}

func filterSearchController(searchBar: UISearchBar) {
    let searchText = searchBar.text ?? ""
    filtered = users.filter { user in
        let isMatchingSearchText =    (user.name?.lowercased().contains(searchText.lowercased()))! || searchText.lowercased().count == 0
        return isMatchingSearchText
    }
    tableView.reloadData()
}

func fetchUsers() {
    let rootRef = Database.database().reference()
    let query = rootRef.child("users").queryOrdered(byChild: "name")
    query.observe(.value) { (snapshot) in
        for child in snapshot.children.allObjects as! [DataSnapshot] {
            if let value = child.value as? NSDictionary {
                let user = User(value as! [String : AnyObject])
                user.id = child.key
                if user.id == Auth.auth().currentUser?.uid {
                    continue
                }
                self.users.append(user)
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }
}

func updateSearchResults(for searchController: UISearchController) {
    filterSearchController(searchBar: searchController.searchBar) 
}

@objc func cancelHandle() {
    dismiss(animated: true, completion: nil)
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return searchController.isActive ? filtered.count : users.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! UserCell

    let user = searchController.isActive ? filtered[indexPath.row] : users[indexPath.row]

    cell.textLabel?.text = user.name
    cell.detailTextLabel?.text = user.email
    cell.backgroundColor = UIColor(r: 243, g: 237, b: 237)
    let backgroundView = UIView()
    backgroundView.backgroundColor = UIColor(r: 245, g: 193, b: 108)
    cell.selectedBackgroundView = backgroundView
    cell.textLabel?.textColor = UIColor(r: 128, g: 57, b: 0)
    cell.detailTextLabel?.textColor = UIColor(r: 170, g: 83, b: 14)
    cell.textLabel?.font = UIFont(name:"Trebuchet MS", size:18)
    cell.detailTextLabel?.font = UIFont(name:"Trebuchet MS", size:12)

    if let profileImageUrl = user.profileImageUrl {
        cell.profileImageView.loadImageUsingCacheWithURLString(urlString: profileImageUrl)
    }

    return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 72
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    dismiss(animated: true) {
    let user = self.filtered[indexPath.row]
    self.messagesController?.showChatControllerForUser(user)
    }
}

}

0 个答案:

没有答案