gpt4 book ai didi

ios - 如何存储单元格的选择,需要最佳实践

转载 作者:行者123 更新时间:2023-11-28 10:40:17 25 4
gpt4 key购买 nike

情况

有一个 TableView 显示文章列表:文章。文章是一个结构对象,是文章列表,从 api 响应中初始化。

我们需要添加一个功能,当用户点击一个单元格时,它会更改为选中状态。为指示选择,我向所选项目单元格添加了复选标记。

cell.accessoryType = .checkmark

问题

问题

问题来了。由于单元格被回收,每次选定的文章单元格被取出时,我们需要存储选择的单元格。

struct Article 在整个应用中使用,因此我们不应该只是添加 isSelected 字段

struct Article {
let title: String
var isSelected: Bool // <- Not good. Article used over the app.
}

临时解决方案

我使用的一些解决方案是制作 bool 数组来存储选择的行。

var selectedRow: Bool = {
return articles.map { return false }
}()

但这种方法看起来很奇怪,而且容易受到细胞插入和删除的影响。

问题

有没有更好的解决方案?


奇怪的解决方法代码

import UIKit

struct Article {
let title: String
}

class SampleTableViewController: UITableViewController {

// MARK: - Properties

let articles: [Article] = {
var tempArticles: [Article] = []
for i in 0...30 {
tempArticles.append(Article(title: "Article at index:\(i)"))
}
return tempArticles
}()

lazy var selectedRow: [Bool] = {
return articles.map { _ in false }
}()

let cellId = "cell"



// MARK: - Lifecycle

override func viewDidLoad() {
super.viewDidLoad()

tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
}




// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return articles.count
}



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
cell.textLabel?.text = articles[indexPath.row].title

// Set selected state to a cell
cell.accessoryType = selectedRow[indexPath.row] ? .checkmark : .none
return cell
}


// MARK: - Table view delegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Update selection
// This isn't the best way to update selection but just to make the example simple.
selectedRow[indexPath.row] = !selectedRow[indexPath.row]
tableView.reloadData()
}
}

enter image description here

最佳答案

解决此问题的一种方法:为 TableView 提供它自己的模型,用于保存列表的状态,包括所选状态。

class ListItem {
let article: Article
var isSelected: Bool
}

然后从这些项目的数组构建您的 TableView :

var listItems = [ListItem]()

用包含您文章的 ListItems 填充该数组并将其用作数据源。

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listItems.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
let item = listItems[indexPath.row]
cell.textLabel?.text = item.article.title
cell.accessoryType = item.isSelected ? .checkmark : .none
return cell
}

最后,在 didSelectRowAtIndexPath 中,您只需设置所选列表项的新 isSelected 值即可。

关于ios - 如何存储单元格的选择,需要最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50755104/

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