我想要完成的任务:
我有一个使用JSON填充的tableview,它从mySQL数据库获取其数据。要解码这个JSON,我使用以下结构:
import UIKit
struct Section {
let name : String
var items : [Portfolios]
}
struct Portfolios: Decodable {
let person: String
let code: String
let tick: Int
var isSelected = false
enum CodingKeys : String, CodingKey {
case person, code, tick
}
}每次选择和取消选定行时,都必须发生两件事:
选择
(1.)添加到选定行的标记附件。
)执行选择()函数。
取消当选的
( 1.)标记附件被移除到选定的行。
执行2.) Unselect()函数。
例如: selectRow()和unselectRow()函数是什么样子的:
func selectRow() {
let url = "https://example.com/example/"
let parameters: Parameters = ["code": codeSelected]
Alamofire.request(url, method: .post, parameters: parameters).responseString { response in
print(response)
}
}注意:此函数将参数发送到后端脚本,并将该代码的“滴答”列值从1更改为2。
当取消选中一行时,Unselect()函数看起来是相同的,但是可以通过将勾号从2更改为1来更新记录。
unselectRow()如下所示:
func unselectRow() {
let url = "https://example.com/example/"
let parameters: Parameters = ["code": codeUnselected]
Alamofire.request(url, method: .post, parameters: parameters).responseString { response in
print(response)
}
}为了提供codeSelected的值,我最初使用了didDeselectRowAt和didSelectRowAt
ex:
structure = sections[indexPath.section].code
let theStructure = structure[indexPath.row]
rmaUnselected = theStructure.code使用didDeselectRowAt和didSelectRowAt并没有引起任何问题,直到我意识到当滚动到足够远的时候,标记附件就消失了。
另一个注意事项是,如果选择了一行,则会更改该记录的代码值,一旦表视图加载并需要已经选中该行,就需要在表视图中显示该行的复选标记附件。
我在willDisplay cell中使用了以下内容
if (theStructure.tick == 2) {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
cell.accessoryType = .checkmark
} else {
tableView.deselectRow(at: indexPath, animated: false)
cell.accessoryType = .none
}我尝试过的:
我试图更改选定和取消选择单元时发生的情况,但我仍然需要select()和unselect()函数中的代码才能工作。
In cellForRowAt I根据isSelected设置复选框
let item = sections[indexPath.section].items[indexPath.row]
cell.accessoryType = item.isSelected ? .checkmark : .none和我didSelectRowAt中的以下内容:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
sections[indexPath.section].items[indexPath.row].isSelected.toggle()
tableView.reloadRows(at: [indexPath], with: .none)
}发布于 2019-09-19 14:26:50
最后一次尝试:
由于选择是由tick指示的,isSelected属性实际上是多余的,但是您可以使用它将Int值转换为更合适的Bool。为了能够修改tick,它是可变的。
请以单数形式命名Portfolio
struct Portfolio: Decodable {
let person: String
let code: String
var tick: Int
var isSelected : Bool {
get { return tick == 2 }
set { tick = newValue ? 2 : 1 }
}
enum CodingKeys : String, CodingKey {
case person, code, tick
}
}将selectRow替换为接受Portfolio参数和indexPath的updateSelection(of:at:)。只有当网络请求成功时,表视图才会重新加载。我不知道阿拉莫火请求返回的字符串是什么。首先更新数据库,然后更新Portfolio将更加安全。
func updateSelection(of portfolio: Portfolio, at indexPath: IndexPath) {
let url = portfolio.isSelected ? "https://example.com/example/selected" : "https://example.com/example/unselected"
let parameters: Parameters = [portfolio.code: portfolio.tick]
Alamofire.request(url, method: .post, parameters: parameters).responseString { response in
switch response.result {
case .success(let string): self.tableView.reloadRows(at: [indexPath], with: .none)
case .failure(let error): print(error)
}
}
}在didSelectRowAt中切换isSelected (它隐式地更新tick )并更新数据库。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
sections[indexPath.section].items[indexPath.row].isSelected.toggle()
let portfolio = sections[indexPath.section].items[indexPath.row]
updateSelection(of: portfolio, at: indexPath)
}您可以将代码留在cellForRow中
let item = sections[indexPath.section].items[indexPath.row]
cell.accessoryType = item.isSelected ? .checkmark : .none但是删除方法willDisplayCell
https://stackoverflow.com/questions/58012819
复制相似问题