Ограничения на автопрокладку, изменяющиеся при перезагрузке представления - Swift - PullRequest
0 голосов
/ 09 ноября 2018

недавно в поисковике VC моего приложения у меня были серьезные проблемы с ограничениями в течение нескольких недель. Я переделал их 4 раза без каких-либо улучшений. Моя ячейка табличного представления не сложна - imageView расположен слева и две метки друг над другом. Размер большинства клеток в порядке. Единственное, что (без рисунка) две ячейки случайно перепутаны (метки и imageView перемещаются далеко не к месту). Вы можете увидеть здесь: enter image description here enter image description here

Вот изображение ограничений для vc:

enter image description here

Странно то, что это иногда происходит, когда я переключаю кнопки видимости вверх. Я посмотрел в своем коде, чтобы посмотреть, смогу ли я это исправить, когда scopeButton перезагрузит табличное представление, но я не смог найти проблемный экземпляр. Мой код ниже:

import UIKit
import Firebase
import Kingfisher

class SearchPostsController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    var idArray:[String] = []

    var detailViewController: DetailViewController? = nil
    var candies = [Person]()
    var filteredCandies = [Person]()
    let searchController = UISearchController(searchResultsController: nil)

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if #available(iOS 11.0, *) {
            navigationItem.hidesSearchBarWhenScrolling = false
            //initiate the search bar that appears up top when view is segued to
        }
        if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
            self.tableView.deselectRow(at: selectionIndexPath, animated: animated)

        }
        self.tableView.reloadData()
        super.viewWillAppear(animated)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if #available(iOS 11.0, *) {
            navigationItem.hidesSearchBarWhenScrolling = true
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        Database.database().reference()
            .child("\(UserData().mySchool!)/posts")
            .queryOrderedByKey()//keys were out of order, so we have to use this to help
            .observeSingleEvent(of: .value, with: { (snapshot) in
                print(snapshot.childrenCount)
                for child in snapshot.children.allObjects as! [DataSnapshot] {
                    print(child.key)
                    self.idArray.append(child.key)
                }
                var reversedNames = [String]()
                for arrayIndex in 0..<self.idArray.count {
                    reversedNames.append(self.idArray[(self.idArray.count - 1) - arrayIndex])
                    //reverse names so we dont have to sort the cells by date
                }
                for x in reversedNames{
                    self.searchNames(id: x)//get names from the ids here, tada!!!
                    self.tableView.reloadData()
                }
            })

        //self.tableView.reloadData()//without this, the results wouldnt show up right away
    searchController.searchBar.setScopeBarButtonTitleTextAttributes([NSAttributedStringKey.foregroundColor.rawValue: UIColor.white], for: .normal)

        searchController.searchBar.scopeButtonTitles = ["Posts", "Users"]
        searchController.searchBar.delegate = self

        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search posts or usernames"
        searchController.searchBar.showsScopeBar = true
        navigationItem.searchController = searchController
        definesPresentationContext = true

        if let splitViewController = splitViewController {
            let controllers = splitViewController.viewControllers
            detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
        }

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 74
    }

//    override func viewWillDisappear(_ animated: Bool) {
//        candies.removeAll()
//    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DAY69:GRADUATION DAY", for: indexPath) as! SeachCell
        cell.cellImageVIew.frame.size.width = 48
        cell.cellImageVIew.frame.size.height = 48

        let personUser: Person
        if isFiltering() {
            personUser = filteredCandies[indexPath.row]
        } else {
            personUser = candies[indexPath.row]
        }
        //PERSON USER IS IMPORTANT!!!!! ^^^
        cell.nameLabel.text = personUser.name.removingPercentEncoding
        cell.messageLabel.text = personUser.category.removingPercentEncoding

        let name = personUser.name
        Database.database().reference().child("users/\(name)/profileImageURL").observe(.value, with: { (snapshot) in
            let profURL = "\(snapshot.value!)"
            let profIRL = URL(string: profURL)
            //set up imageview
            cell.cellImageVIew.layer.borderWidth = 1
            cell.cellImageVIew.layer.masksToBounds = false
            cell.cellImageVIew.layer.borderColor = UIColor.black.cgColor
            cell.cellImageVIew.layer.cornerRadius = cell.cellImageVIew.frame.height/2
            cell.cellImageVIew.clipsToBounds = true
            cell.cellImageVIew.contentMode = .scaleAspectFill
            cell.cellImageVIew.kf.indicatorType = .activity
            cell.cellImageVIew.kf.setImage(with: profIRL)

        })
        //TODO: make an extension of imageview to do all this for me. It's getting to be ridiculous
        return cell
    }

    func searchNames(id: String){
//        var message = String()
//        var name = String()
        Database.database().reference().child("\(UserData().mySchool!)/posts/\(id)/message").observe(.value, with: { (snapshot) in
//             message = snapshot.value as! String

        Database.database().reference().child("\(UserData().mySchool!)/posts").child("\(id)/username").observe(.value, with: { (username) in
//            name = username.value as! String
            let user = Person(category: "\(snapshot.value!)", name: "\(username.value!)", id: id)
            self.candies.append(user)
            print( "\(snapshot.value!)", "\(username.value!)")
            self.tableView.reloadData()

        })
        })

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isFiltering() {
            return filteredCandies.count
        }

        return candies.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        tableView.estimatedRowHeight = 74.0
        tableView.rowHeight = UITableViewAutomaticDimension
        return UITableViewAutomaticDimension
    }
//    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
//        return 74
//    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DAY69:GRADUATION DAY", for: indexPath) as! SeachCell
        cell.cellImageVIew.frame.size.width = 48
        cell.cellImageVIew.frame.size.height = 48

        let searchBar = searchController.searchBar
        let scope = searchBar.scopeButtonTitles?[searchBar.selectedScopeButtonIndex]

        if scope == "Users"{
            let username = candies[indexPath.row].name
            print(username)
            self.tableView.deselectRow(at: indexPath, animated: true)
            performSegue(withIdentifier: "userClicked", sender: username)

        }
        if scope == "Posts"{
            let post = candies[indexPath.row].category
            let user = candies[indexPath.row].name
            let id = candies[indexPath.row].id
            print(post)
            let defaults = UserDefaults.standard
            defaults.set(id, forKey: "ID")
            let def2 = UserDefaults.standard
            def2.set(post, forKey: "Post")
            def2.set(user, forKey: "USER")
            self.tableView.deselectRow(at: indexPath, animated: true)
            performSegue(withIdentifier: "postCellTapped", sender: nil)
        }

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    //__________tbv methods above________________________________________________
    func searchBarIsEmpty() -> Bool {
        // Returns true if the text is empty or nil
        return searchController.searchBar.text?.isEmpty ?? true
    }

    func filterContentForSearchText(_ searchText: String, scope: String = "All") {
        filteredCandies = candies.filter({(candy : Person) -> Bool in
            let doesCategoryMatch = (scope == "Posts") || (scope == "Users")
            print(searchText)

            if searchBarIsEmpty() {
                return doesCategoryMatch
            }
            if scope == "Users"{
                return doesCategoryMatch && candy.name.lowercased().contains(searchText.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!.lowercased())
            }
            else{
                return doesCategoryMatch && candy.category.lowercased().contains(searchText.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!.lowercased())
            }

        })
        tableView.reloadData()

    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let personalUser: Person
                if isFiltering() {
                    personalUser = filteredCandies[indexPath.row]
                } else {
                    personalUser = candies[indexPath.row]
                }

            }
        }
        if segue.identifier == "userClicked" {
            if let nextView = segue.destination as? UserProfileController {
                nextView.selectedUser = "\(sender!)"
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func isFiltering() -> Bool {
    let searchBarScopeIsFiltering = searchController.searchBar.selectedScopeButtonIndex != 0
    return searchController.isActive && (!searchBarIsEmpty() || searchBarScopeIsFiltering)
    }
}
extension SearchPostsController: UISearchResultsUpdating {
    // MARK: - UISearchResultsUpdating Delegate
    func updateSearchResults(for searchController: UISearchController) {
        let searchBar = searchController.searchBar
        let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
        filterContentForSearchText(searchController.searchBar.text!, scope: scope)
    }
}
extension SearchPostsController: UISearchBarDelegate {
    // MARK: - UISearchBar Delegate
    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
    }
}

Любая помощь будет принята с благодарностью при попытке устранить ограничения! Спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...