在Swift中的UITableView中搜索时禁用scrollViewDidScroll方法

时间:2017-06-19 11:34:03

标签: ios swift uitableview

当tableView确实滚动到底部时,我实现了scrollViewDidScroll以更新UITableView。并添加10个新元素 UISearchbar中也有一个UITableView。当我搜索某些内容时,可能需要滚动。但是我不想在这种情况下执行scrollViewDidScroll

override func scrollViewDidScroll(scrollView: UIScrollView) {
        let height = scrollView.frame.size.height
        let contentYoffset = scrollView.contentOffset.y
        let distanceFromBottom = scrollView.contentSize.height - contentYoffset
        if distanceFromBottom < height {
            print(" you reached end of the table")
            self.fetchOffset += 10
            // changing fillteredPosts. updating tableView.
        }
    }

func updateSearchResultsForSearchController(searchController: UISearchController) {
        filteredPosts.removeAll()
        let searchText = searchController.searchBar.text
        if let searchString = searchText, let searchInt = Int(searchString) {
            for post in originalAllPosts {
                if post.userId == searchInt {
                    filteredPosts.append(post)
                }
            }
        } 
        tableView.reloadData()
    }

1 个答案:

答案 0 :(得分:1)

假设您正在搜索输入的每个字符

    override func scrollViewDidScroll(scrollView: UIScrollView) {

        if searchController.searchBar.text.isEmpty {
            let height = scrollView.frame.size.height
            let contentYoffset = scrollView.contentOffset.y
            let distanceFromBottom = scrollView.contentSize.height - contentYoffset
            if distanceFromBottom < height {
                print(" you reached end of the table")
                self.fetchOffset += 10
                // changing fillteredPosts. updating tableView.
            }
         }

    }

此处scrollViewDidScroll内的语句只有在searchBar为空时才会执行,这意味着我们没有执行搜索操作