首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当滚动桌面视图时配件消失

当滚动桌面视图时配件消失
EN

Stack Overflow用户
提问于 2019-09-19 14:03:43
回答 1查看 83关注 0票数 1

我想要完成的任务:

我有一个使用JSON填充的tableview,它从mySQL数据库获取其数据。要解码这个JSON,我使用以下结构:

代码语言:javascript
复制
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()函数是什么样子的:

代码语言:javascript
复制
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()如下所示:

代码语言:javascript
复制
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的值,我最初使用了didDeselectRowAtdidSelectRowAt

ex:

代码语言:javascript
复制
    structure = sections[indexPath.section].code
    let theStructure = structure[indexPath.row]
    rmaUnselected = theStructure.code

使用didDeselectRowAtdidSelectRowAt并没有引起任何问题,直到我意识到当滚动到足够远的时候,标记附件就消失了。

另一个注意事项是,如果选择了一行,则会更改该记录的代码值,一旦表视图加载并需要已经选中该行,就需要在表视图中显示该行的复选标记附件。

我在willDisplay cell中使用了以下内容

代码语言:javascript
复制
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设置复选框

代码语言:javascript
复制
let item = sections[indexPath.section].items[indexPath.row]
cell.accessoryType = item.isSelected ? .checkmark : .none

和我didSelectRowAt中的以下内容:

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    sections[indexPath.section].items[indexPath.row].isSelected.toggle()
    tableView.reloadRows(at: [indexPath], with: .none)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-19 14:26:50

最后一次尝试:

由于选择是由tick指示的,isSelected属性实际上是多余的,但是您可以使用它将Int值转换为更合适的Bool。为了能够修改tick,它是可变的。

请以单数形式命名Portfolio

代码语言:javascript
复制
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参数和indexPathupdateSelection(of:at:)。只有当网络请求成功时,表视图才会重新加载。我不知道阿拉莫火请求返回的字符串是什么。首先更新数据库,然后更新Portfolio将更加安全。

代码语言:javascript
复制
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 )并更新数据库。

代码语言:javascript
复制
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

代码语言:javascript
复制
let item = sections[indexPath.section].items[indexPath.row]
cell.accessoryType = item.isSelected ? .checkmark : .none

但是删除方法willDisplayCell

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58012819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档