0
所以我是新來的swift,我一直有一些問題讓我所有的tableview單元格執行segue後取消選擇。現在所有的單元格在我執行segue之後都會保持突出顯示,除了最上面的單元格之外。最上面的一個也是可選擇的,並且可以執行搜索,但它沒有突出顯示。Swift - Tableview單元格保持突出顯示 - 除了頂部之一 - 沒有任何問題
這是奇怪的行爲,但我已經嘗試了所有的基礎知識。
我已經試過
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)
}
}
我也嘗試過在賽格瑞方法的準備。
我也試了一下從細胞:cell.selectionStyle = .none
我也試着在故事板將「選擇」無法比擬的。
沒有什麼似乎改變了行爲,所以我很茫然。我想我已經搞亂了某個地方,找不到它是什麼。
這是我的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
}
}
}
}
嗨inokey是真的,謝謝你的幫助。我的問題原來有點不同。我正在從cellForRowAtIndex返回一個通用的UITableViewCell()。我應該已經回來了可重複使用的「細胞」。我解決了這個問題,問題就消失了。 –
如果這是問題,那麼你現在應該關閉這個問題@DarkhorseFantasySports –