ios - 在uitableview中过滤后执行segue
问题描述
我目前正在创建一个在 uitableview 上有位置列表的应用程序。还有一个过滤功能,可以根据类别过滤位置。当不过滤时,uitableviewcell 会在单击时执行 segue,并且正确的数据会加载到目标视图控制器中。但是,当我过滤 tableview 时,单元格会填充正确的数据,但是当我单击单元格时,目标 viewcontroller 总是从 UNFILTERED tableview 中的第一个单元格加载数据。
我正在使用的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "shoutoutCell", for: indexPath) as! shoutoutTableViewCell
var shout = shoutout[indexPath.row]
if isFiltering == true {
shout = filteredShoutout[indexPath.row]
} else {
shout = shoutout[indexPath.row]
}
cell.shoutout = shout
cell.showsReorderControl = true
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var object = shoutout[indexPath.row]
if isFiltering == true {
object = filteredShoutout[indexPath.row]
} else {
object = shoutout[indexPath.row]
}
performSegue(withIdentifier: "shoutoutDetailSegue", sender: object)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "shoutoutDetailSegue" {
if let destination = segue.destination as? shoutoutDetailViewController {
// destination.shoutoutSelected = sender as! object
destination.shoutoutSelected = shoutout[(tableView.indexPathForSelectedRow?.row)!]
}
}
}
解决方案
发生这种情况是因为即使在过滤时您也在使用shoutout
Array 。prepare(for segue
destination.shoutoutSelected = shoutout[(tableView.indexPathForSelectedRow?.row)!]
将其更改为
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "shoutoutDetailSegue" {
if let destination = segue.destination as? shoutoutDetailViewController {
// Here `Model` should be your class or struct
destination.shoutoutSelected = sender as! Model
}
}
或将您的逻辑从didSelectRowAt
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "shoutoutDetailSegue" {
if let destination = segue.destination as? shoutoutDetailViewController {
if isFiltering == true {
object = filteredShoutout[indexPath.row]
} else {
object = shoutout[indexPath.row]
}
destination.shoutoutSelected = object
}
}
推荐阅读
- python - 如何将信息从项目传递到 django 中的模态以对其进行编辑?
- python - 在python中循环多个变量(并非所有变量都是有限甚至已知的序列)
- pandas - Repl.it 无法安装 pandas
- c# - .net 被动 mvp 实施 w 统一新手问题
- latex - 将目录页字体大小和章节、节和小节字体大小分别设置为值
- php - 注意:$_SESSION['xxxxx'] 处的未定义索引
- javascript - TypeError:无法读取 Brain.js 中未定义的属性“权重”
- c# - 有没有办法在 C++ 中使用 linux 上的 windows 窗体(已关闭)
- python - FastAPI - 在后台运行连续 GET 请求的最佳方式
- amazon-web-services - 如何使 AWS Cognito(托管 UI)的 redirect_uri 进行身份验证?