gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:21:37 25 4
gpt4 key购买 nike

我实现了 scrollViewDidScroll 以便在 tableView 确实滚动到底部时更新 UITableView。并向其中添加 10 个新元素。
UITableView 中也有一个UISearchbar。当我搜索某些内容时,可能需要滚动。但我不想在这种情况下执行 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()
}

最佳答案

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

    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 为空时执行,这意味着我们没有执行搜索操作

关于ios - 在 Swift 的 UITableView 中搜索时禁用 scrollViewDidScroll 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44629473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com