我知道主题问题听起来很复杂。我会尽力简化我遇到的问题。
我的 UITableView 中有 UIButton。 UITableViewCell 是为 UITableView 配置的。虽然,我在我的 UIViewController 中使用 UIButton 操作。我通过简单地更改行高和一些优先级约束操作,为 UITableViewCells 设置了折叠/展开。当展开时,单元格下方会出现额外的信息。到目前为止,一切工作都完美无缺。
当我单击 UIButton 折叠默认状态时,我将 UIButton 图像更改为另一个图像。然后当我单击单元格以展开 UIButton 图像时,它会返回到初始图像。反之亦然。所以,我猜测每当我更改行的高度时,UITableView 都会重新创建单元格,因此之前所做的更改不会显示。
相关代码如下:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "FavCell", for: indexPath) as? FavCell else { return UITableViewCell() }
if let data = UserDefaults.standard.data(forKey: selectedCur),
let storedCurrencies = NSKeyedUnarchiver.unarchiveObject(with: data) as? [SelectedCurrency] {
selectedCurrencies = storedCurrencies
// SORT BY RANK
selectedCurrencies.sort(by: { $0.rank! < $1.rank! })
if selectedCurrencies.count > 0 {
// CATCH ANY NIL VALUES AND PREVENT APP CRASHES
let noVal = selectedCurrencies[indexPath.row].rank ?? 0
let nameVal = selectedCurrencies[indexPath.row].name ?? "N/A"
let symbolVal = selectedCurrencies[indexPath.row].symbol ?? "N/A"
cell.configureCell(no: "\(noVal)", name: nameVal, symbol: symbolVal)
}
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if collapseIndex == indexPath.row {
collapseIndex = -1
} else {
collapseIndex = indexPath.row
}
tableView.beginUpdates()
tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
tableView.endUpdates()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if collapseIndex == indexPath.row {
return 280 // When collapsed cell view displayed - Number will be adjusted after
} else {
return 120 // Height of initial cell contents that fits
}
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
以下是我更改 UIButtons 图像的方法:
class NotificationBtn: UIButton {
var bellToggle = Int()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if bellToggle == 1 {
self.transform = CGAffineTransform(scaleX: 1.25, y: 1.25) // Bounce
self.setImage(UIImage(named: "bell_off"), for: .normal)
UIView.animate(withDuration: 1.2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: .allowUserInteraction, animations: { self.transform = CGAffineTransform.identity }, completion: nil)
curImg = self.image(for: .normal)!
bellToggle = 2
super.touchesBegan(touches, with: event)
} else {
self.transform = CGAffineTransform(rotationAngle: 25 * 3.14/180.0) // Rotate via 25 degree - Bell ringing animation
self.setImage(UIImage(named: "bell_on"), for: .normal)
UIView.animate(withDuration: 1.2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: .allowUserInteraction, animations: { self.transform = CGAffineTransform.identity }, completion: nil)
curImg = self.image(for: .normal)!
bellToggle = 1
super.touchesBegan(touches, with: event)
}
}
}
感谢您的宝贵时间。
最佳答案
你猜对了。当您调用 reloadRows
方法时,将调用 cellForRowAt
并将要重新加载的单元格替换为另一个单元格。单元格被替换,因此出现的按钮是另一个按钮。这就是为什么您的按钮图像错误。
要解决此问题,请在 cellForRowAt
方法中检查并使用 collapseIndex
更新您的按钮图像。我认为它会起作用。例如,在 cellForRowAt
中,在创建单元格之后
if collapseIndex == indexPath.row {
// Updated button with collapse state image
} else {
// Updated button with uncollapse state image
}
关于ios - 当 UITableViewCell 使用 Swift 展开时如何更改 UIButton 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47311239/