gpt4 book ai didi

ios - 选择表格中的单元格无法正常工作

转载 作者:行者123 更新时间:2023-11-28 21:20:08 26 4
gpt4 key购买 nike

我在 TableViewController 中有一个静态表。每当我选择一行时,它都会用灰色填充整个单元格。它对我点击的每一行都这样做。如果我使用:

cell.selectionStyle = .none

它会使单元格填充为白色。

Tableview 属性检查器:

TableView Attributes Inspector

TableViewCell 属性检查器:

TableViewCell Attributes Inspector

TableView Controller :

import UIKit

class OptionTableViewController: UITableViewController {

@IBOutlet var optionsTable: UITableView!

let defaults = UserDefaults.standard

let numberOfRows = [7,2]

let cellIdentifier = "OptionCells"

override func viewDidLoad() {
super.viewDidLoad()

optionsTable.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that con be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
var rows = 0
if(section < numberOfRows.count){
rows = numberOfRows[section]
}
return rows
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.selectionStyle = .none
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

cell.textLabel?.text = optionSelections[indexPath.row]
return cell
}
}

我相信我已正确设置所有内容。为什么当我点击它们时它会填充单元格?

更新:我更新了代码以显示我目前拥有的内容。

更新 2:我附上了几张图片,以显示表格在点击每个其他单元格之前和之后的样子。

更新 3:我正在出列单元格以将附件类型更改为复选标记。

之前: Before Tapping Cells

之后: After Tapping Every Other Cell

最佳答案

正如评论中所讨论的,我认为问题是 didSelectRow(at:) 方法中单元格出列的症状:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
}

你应该改用

    let cell = tableView.cellForRow(at: indexPath)

获取当前位于给定 indexPath 的单元格(请注意,如果该 indexPath 已被滚动关闭或从未出现在屏幕上,它可能会返回 nil)。出队让一个未使用的单元格进入给定的 indexPath -(我认为)然后它位于现有单元格的前面 - 因此出现了奇怪的行为。

关于ios - 选择表格中的单元格无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40046092/

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