当UITableViewCell使用Swift未失效时,如何更改UIButton图像



我知道主题问题听起来很复杂。我将尽我所能简化我的问题。

我的UITableView中有UIButton。为UITableView配置了UITableViewCell。尽管如此,我在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
}

最新更新