【问题标题】:How to also change UIButton Image when UITableViewCell uncollapsed using Swift?当 UITableViewCell 使用 Swift 展开时如何更改 UIButton Image?
【发布时间】:2017-11-15 15:27:32
【问题描述】:

我知道主题问题听起来很复杂。我会尽力简化我遇到的问题。

我的 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)
    }
}
}

感谢您的宝贵时间。

【问题讨论】:

  • 你能展示你的cellForRow方法吗?
  • 按要求添加。
  • 你更新了这个方法configureCell中的按钮图片了吗?
  • 不,我使用 NotifcationBtn: UIButton 类来应用更改。
  • 我已经添加了一个答案。你怎么看?

标签: ios uitableview uibutton tableview


【解决方案1】:

你猜对了。当您调用reloadRows 方法时,将调用cellForRowAt 并将您要重新加载的单元格替换为另一个单元格。单元格被替换,因此出现的按钮是另一个按钮。这就是为什么你有错误的按钮图像。

要修复它,在cellForRowAt 方法中,检查并使用collapseIndex 更新您的按钮图像。我认为它会起作用。比如cellForRowAt里面,创建cell后

if collapseIndex == indexPath.row {
    // Updated button with collapse state image
} else {
    // Updated button with uncollapse state image
}

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    相关资源
    最近更新 更多