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

标签 ios uitableview swift3 didselectrowatindexpath

我在 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/

相关文章:

IOS - UICollectionView - 通过 segue 将数据传递给 subview

ios - swift 。公共(public)协议(protocol)中的内部类型

swift - 尝试为 UICollectionView Swift 添加 header 时出错

iphone - 警告 : Attempt to present <CustomViewController> on <UINavigationontroller> whose view is not in the window hierarchy

ios - 访问 ScrollView 委托(delegate)的父组件

ios - UITableView 及其在 xib 文件中的单元格

swift - 动态加载数据uitableview swift

swift - 在 Swift 3 中设置明天的时间

ios - Swift 不支持 SDK 'iPhoneSimulator9.3.sdk'?

ios - var 和 let 定义改变了 swift 和 swift2。 swift 2 中的 var 定义是否发生了变化?