gpt4 book ai didi

Swift - Tableview 单元格保持突出显示 - 除了最上面的一个 - 无论如何

转载 作者:行者123 更新时间:2023-11-28 15:44:53 24 4
gpt4 key购买 nike

所以我是 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/

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