- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我是 swift 的新手,我在执行 segue 后让我的所有 tableview 单元格取消选择时遇到了一些问题。现在,在我执行 segue 后,所有单元格都保持突出显示,除了顶部的单元格。最上面的也是可选的并执行 segue,但它根本没有突出显示。
这是一种奇怪的行为,但我已经尝试了所有的基础知识。
我试过了
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
我试过了
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
self.tableView.deselectRow(at: selectionIndexPath, animated: animated)
}
}
我也在 prepare for segue 方法中尝试过。
我也在单元格中尝试过:cell.selectionStyle = .none
我还尝试将 Storyboard中的“选择”更改为无。
似乎没有什么能改变行为,所以我不知所措。我想我搞砸了某处的东西,找不到它是什么。
如果有人想看的话,这是我的完整的 tableview 类。
import UIKit
import Firebase
class DraftListCell : UITableViewCell {
@IBOutlet weak var playerName: UILabel!
@IBOutlet weak var playerPrice: UILabel!
@IBOutlet weak var priceRemaining: UILabel!
@IBOutlet weak var vsLabel: UILabel!
@IBOutlet weak var injuredLabel: UILabel!
@IBOutlet weak var gameTimeLabel: UILabel!
}
class NBADraftList: UIViewController, UITableViewDelegate, UITableViewDataSource, FIRDatabaseReferenceable{
@IBOutlet var tableView: UITableView!
let cellReuseIdentifier = "cell"
var ref: FIRDatabaseReference!
var players = [Player]()
let formatter = NumberFormatter()
override func viewDidLoad() {
let leftBarButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(myLeftSideBarButtonItemTapped(_:)))
self.title = "Select"
self.navigationItem.leftBarButtonItem = leftBarButton
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
self.tableView.rowHeight = 100.0
self.tableView.tableFooterView = UIView()
tableView.delegate = self
tableView.dataSource = self
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 0
ref = FIRDatabase.database().reference().child("NBATodaysPlayers")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
var players = [Player]()
for player in snapshot.children {
players.append(Player(snapshot: player as! FIRDataSnapshot))
}
self.players = players.sorted(by: { $0.Value > $1.Value })
self.tableView.reloadData()
}) { (error) in
print("error")
}
super.viewDidLoad()
}
func myLeftSideBarButtonItemTapped(_ sender:UIBarButtonItem!)
{
self.dismiss(animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return players.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DraftListCell", for: indexPath as IndexPath) as! DraftListCell
if let formattedPrice = formatter.string(from: players[indexPath.row].Value as NSNumber) {
cell.playerPrice.text = "Game Price: \(formattedPrice)"
}
if let formattedRemainingPrice = formatter.string(from: 10000 - players[indexPath.row].Value as NSNumber) {
cell.priceRemaining.text = "Remaining: \(formattedRemainingPrice)"
}
cell.playerName.text = players[indexPath.row].Name
cell.injuredLabel.text = players[indexPath.row].Inj
cell.vsLabel.text = players[indexPath.row].visiting + " @ " + players[indexPath.row].home
cell.gameTimeLabel.text = players[indexPath.row].game_time
cell.textLabel?.textColor = .white
cell.backgroundColor = .black
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "BuyStats", sender: indexPath);
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "BuyStats" {
let buyStats = segue.destination as! BuyStats
if let indexPath = tableView.indexPathForSelectedRow {
let player = players[indexPath.row]
buyStats.selectedPlayer = player
}
}
}
最佳答案
在您的 cellForRowAt
中添加以下内容:
if cell.isSelected == true {
tableView.deselectRow(at: indexPath, animated: false)
}
或者,如果您想通过 Segue 专门执行此操作。您可以设置一个全局变量,比方说 deselectAll
并在 cellForRowAt
希望对你有帮助
关于Swift - Tableview 单元格保持突出显示 - 除了最上面的一个 - 无论如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43262249/
我认为这应该不是一个大问题,但我自己找不到解决方案。一如既往:p 我有一个 UIWebView,其背景颜色设置为clearColor,但是当我尝试向下滚动太多时,我会在加载的 HTML 上方看到深灰色
我注意到,每当我重新安装我的应用程序时,IdentifierForVendor 都会不断变化。有没有办法让我的设备拥有相同的标识符?问题是,我需要确保标识符相同,因为我有一个备份系统,即使在删除并重新
一切都在标题中。 我有一个带有单元格的 UITableView。 cells 有一个Shadow (self.layer.shadow...)。 问题是一个单元格的阴影重叠上方的单元格。我怎样才能防止
我正在尝试创建一个切换开关,您可以在其中点击一侧,然后背景会滑过以使该侧“处于事件状态”。 为了适应可变宽度,我使用 display: table 设置了切换的两侧。这很好用。然后,我将第三个 div
我是一名优秀的程序员,十分优秀!