我有一个带有自定义单元格resultsCell
的UITableView。它包含4个UILabel和6个UIButton,看起来像这样(我标记了每个框的类型):
UITableView中大约有10个这些单元格,因此当然可以重复使用这些单元格。每当我上下滚动多次时,单元格中的每个UILabel都会完美地更新为每个单元格,但是,UIButton并不总是正确更新,并且会显示不同的值。每个按钮都链接到IBAction,该IBAction使用segue将用户重定向到其他视图。如何修复不更新的UIButton?还是有更好的方法来实现此目的而无需按钮?
代码:
UITableViewCell类:
class ResultsTableViewCell: UITableViewCell {
@IBOutlet weak var red1: UIButton!
@IBOutlet weak var red2: UIButton!
@IBOutlet weak var red3: UIButton!
@IBOutlet weak var redScore: UILabel!
@IBOutlet weak var blue1: UIButton!
@IBOutlet weak var blue2: UIButton!
@IBOutlet weak var blue3: UIButton!
@IBOutlet weak var blueScore: UILabel!
@IBOutlet weak var teamsLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
override func prepareForReuse() { //I tried doing this with no luck.
super.prepareForReuse()
red1?.setTitle("", for: .normal)
red2?.setTitle("", for: .normal)
red3?.setTitle("", for: .normal)
blue1?.setTitle("", for: .normal)
blue2?.setTitle("", for: .normal)
blue3?.setTitle("", for: .normal)
}
}
UITableView出队可重用单元格代码
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "resultsCell") as! ResultsTableViewCell
let thisMatch = resultsDict[indexPath.row] as! [String:String]
cell.red1?.setTitle(thisMatch["red2"], for: .normal)
cell.red2?.setTitle(thisMatch["red2"], for: .normal)
cell.red3?.setTitle(thisMatch["red3"], for: .normal)
cell.blue1?.setTitle(thisMatch["blue1"], for: .normal)
cell.blue2?.setTitle(thisMatch["blue2"], for: .normal)
cell.blue3?.setTitle(thisMatch["blue3"], for: .normal)
cell.redScore?.text = thisMatch["redScore"]
cell.blueScore?.text = thisMatch["blueScore"]
if (playerID == thisMatch["red1"]) {
cell.red1?.underline()
} else if (playerID == thisMatch["red2"]) {
cell.red2?.underline()
} else if (playerID == thisMatch["red3"]) {
cell.red3?.underline()
} else if (playerID == thisMatch["blue1"]) {
cell.blue1?.underline()
} else if (playerID == thisMatch["blue2"]) {
cell.blue2?.underline()
} else if (playerID == thisMatch["blue3"]) {
cell.blue3?.underline()
}
return cell
}
thisMatch
{
"red1": "1234",
"red2": "4567",
"red3": "8901",
"blue1": "2345",
"blue2": "6789",
"blue3": "0123",
"redScore": "456",
"blueScore": "789",
}
按钮应显示什么:
下划线扩展名:
extension UIButton {
func underline() {
let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSRange(location: 0, length: (self.titleLabel?.text!.count)!))
self.setAttributedTitle(attributedString, for: .normal)
}
}
答案 0 :(得分:0)
每个Button都在重用,因此我们也应该分配不带下划线的大小写。尝试使用此代码。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "resultsCell") as! ResultsTableViewCell
let thisMatch = resultsDict[indexPath.row] as! [String:String]
cell.redScore?.text = thisMatch["redScore"]
cell.blueScore?.text = thisMatch["blueScore"]
if (playerID == thisMatch["red1"]) {
cell.red1?.setTitle(thisMatch["red1"], for: .normal)
cell.red1?.underline()
} else {
cell.red1?.setTitle(thisMatch["red1"], for: .normal)
}
if (playerID == thisMatch["red2"]) {
cell.red2?.setTitle(thisMatch["red2"], for: .normal)
cell.red2?.underline()
}else {
cell.red2?.setTitle(thisMatch["red2"], for: .normal)
}
if (playerID == thisMatch["red3"]) {
cell.red3?.setTitle(thisMatch["red3"], for: .normal)
cell.red3?.underline()
} else {
cell.red3?.setTitle(thisMatch["red3"], for: .normal)
}
if (playerID == thisMatch["blue1"]) {
cell.blue1?.setTitle(thisMatch["blue1"], for: .normal)
cell.blue1?.underline()
} else {
cell.blue1?.setTitle(thisMatch["blue1"], for: .normal)
}
if (playerID == thisMatch["blue2"]) {
cell.blue2?.setTitle(thisMatch["blue2"], for: .normal)
cell.blue2?.underline()
}else {
cell.blue2?.setTitle(thisMatch["blue2"], for: .normal)
}
if (playerID == thisMatch["blue3"]) {
cell.blue3?.setTitle(thisMatch["blue3"], for: .normal)
cell.blue3?.underline()
} else {
cell.blue3?.setTitle(thisMatch["blue3"], for: .normal)
}
return cell
}