英文:
Pagination doesn't set back to first page after search box is cleared
问题
我正在使用Swift进行iOS应用程序开发,并在分页功能中遇到问题。
这个想法非常简单。当输入查询时,搜索函数应该返回所有可能的结果,并使用分页器将它们列出,每页显示10行,如果结果超过10行。用户可以通过点击分页器浏览每个结果页面。然而,当用户清除搜索框或清空搜索框时,它应该始终返回/设置为第一个结果页面(当前分页器应该等于0),并准备好接受下一个输入。
目前出现的问题是,在使用分页器浏览先前查询的结果页面后,当用户清除搜索框以进行新的查询时,它永远不会返回到第一页。结果是,新的查询搜索结果从先前浏览页面的当前页码开始显示,导致忽略了第一页的结果,用户必须逐个使用分页器返回以查看第一页的结果。
当前的代码如下:
import SwiftUI
class DictionaryViewModel: ObservableObject {
@Published var filteredWordPairs: [WordPair] = []
@Published var searchText = "" {
didSet {
filterWordPairs()
}
}
private var allWordPairs: [WordPair] = DataLoader.loadCSVFile()
private var currentPage = 0
private let pageSize = 10
func wordPairsForPage(_ pageIndex: Int, pageSize: Int) -> ArraySlice<WordPair> {
let startIndex = pageIndex * pageSize
let endIndex = min(startIndex + pageSize, filteredWordPairs.count)
if startIndex >= endIndex {
return []
}
return filteredWordPairs[startIndex..<endIndex]
}
var canGoToNextPage: Bool {
return (currentPage + 1) * pageSize < filteredWordPairs.count
}
func gotoPreviousPage() {
currentPage = max(currentPage - 1, 0)
}
func gotoNextPage() {
if canGoToNextPage {
currentPage += 1
}
}
private func filterWordPairs() {
if searchText.isEmpty {
filteredWordPairs = []
} else {
let lowercaseSearchText = searchText.lowercased()
filteredWordPairs = allWordPairs.filter { wordPair in
let lowercasedWord1 = wordPair.Word1.lowercased()
let lowercasedWord2 = wordPair.Word2.lowercased()
return lowercasedWord1.contains(lowercaseSearchText) ||
lowercasedWord2.contains(lowercaseSearchText) ||
lowercasedWord1.range(of: lowercaseSearchText) != nil ||
lowercasedWord2.range(of: lowercaseSearchText) != nil
}
}
currentPage = 0 // 当搜索文本更改时重置currentPage
}
}
以上是你要翻译的内容。
英文:
I'm using Swift for iOS app development and encountering a problem with pagination function.
The idea is quite straightforward. As the query is entered, the search function should return all possible results, and list them 10 rows per page using paginator if the results are more than 10. The user may browse to each result page by clicking paginator. However, it should always goes back/set to the first result page (current paginator should equal to 0) when the search box is cleared or emptied by the user and get ready for next input.
The problem currently occurring is that after browsing the result page of previous query with paginator, it never set back to first page when the search box is cleared for new query. In result, the new query search results started showing from current page number of previous browsed page number, which ended up the results from page 1 are overlooked and the user has to use paginator one by one to go back to see the results in first page.
Current code is as follows:
import SwiftUI
class DictionaryViewModel: ObservableObject {
@Published var filteredWordPairs: [WordPair] = []
@Published var searchText = "" {
didSet {
filterWordPairs()
}
}
private var allWordPairs: [WordPair] = DataLoader.loadCSVFile()
private var currentPage = 0
private let pageSize = 10
func wordPairsForPage(_ pageIndex: Int, pageSize: Int) -> ArraySlice<WordPair> {
let startIndex = pageIndex * pageSize
let endIndex = min(startIndex + pageSize, filteredWordPairs.count)
if startIndex >= endIndex {
return []
}
return filteredWordPairs[startIndex..<endIndex]
}
var canGoToNextPage: Bool {
return (currentPage + 1) * pageSize < filteredWordPairs.count
}
func gotoPreviousPage() {
currentPage = max(currentPage - 1, 0)
}
func gotoNextPage() {
if canGoToNextPage {
currentPage += 1
}
}
private func filterWordPairs() {
if searchText.isEmpty {
filteredWordPairs = []
} else {
let lowercaseSearchText = searchText.lowercased()
filteredWordPairs = allWordPairs.filter { wordPair in
let lowercasedWord1 = wordPair.Word1.lowercased()
let lowercasedWord2 = wordPair.Word2.lowercased()
return lowercasedWord1 .contains(lowercaseSearchText) ||
lowercasedWord2.contains(lowercaseSearchText) ||
lowercasedWord1.range(of: lowercaseSearchText) != nil ||
lowercasedWord2.range(of: lowercaseSearchText) != nil
}
}
currentPage = 0 // Reset currentPage when search text changes
}
}
答案1
得分: 1
当搜索文本发生变化时,你正确地将currentPage重置为0,但是当用户使用分页器浏览页面时,你没有更新currentPage。我认为当searchText为空时,你需要将currentPage重置为0。以下是更新后的代码:
@Published private var currentPage = 0
private func filterWordPairs() {
if searchText.isEmpty {
filteredWordPairs = []
currentPage = 0 // 当搜索文本被清除时,重置currentPage为0
} else {
let lowercaseSearchText = searchText.lowercased()
filteredWordPairs = allWordPairs.filter { wordPair in
let lowercasedWord1 = wordPair.Word1.lowercased()
let lowercasedWord2 = wordPair.Word2.lowercased()
return lowercasedWord1.contains(lowercaseSearchText) ||
lowercasedWord2.contains(lowercaseSearchText) ||
lowercasedWord1.range(of: lowercaseSearchText) != nil ||
lowercasedWord2.range(of: lowercaseSearchText) != nil
}
currentPage = 0 // 当搜索文本发生变化时,重置currentPage为0
}
}
请注意,我只翻译了代码部分,其他内容不包括在内。
英文:
When the search text changes, you correctly reset the currentPage to 0, but you're not updating the currentPage when the user navigates through the pages using the paginator. I think you need to reset currentPage to 0 when searchText.isEmpty. Here's updated code
@Published private var currentPage = 0
private func filterWordPairs() {
if searchText.isEmpty {
filteredWordPairs = []
currentPage = 0 // Reset currentPage when search text is cleared
} else {
let lowercaseSearchText = searchText.lowercased()
filteredWordPairs = allWordPairs.filter { wordPair in
let lowercasedWord1 = wordPair.Word1.lowercased()
let lowercasedWord2 = wordPair.Word2.lowercased()
return lowercasedWord1.contains(lowercaseSearchText) ||
lowercasedWord2.contains(lowercaseSearchText) ||
lowercasedWord1.range(of: lowercaseSearchText) != nil ||
lowercasedWord2.range(of: lowercaseSearchText) != nil
}
currentPage = 0 // Reset currentPage when search text changes
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论