【问题标题】:Swift - How to jump to the top using becomeFirstResponderSwift - 如何使用 becomeFirstResponder 跳转到顶部
【发布时间】:2019-11-04 07:58:26
【问题描述】:

这就是我想要做的,当我点击我的button 时,我想跳到顶部,但是当我点击button 时,我的应用程序崩溃了。

@IBAction func addToCart(_ sender: UIButton) {

    if quantitylbl.text == "0" {
        dismiss(animated: true)
    }

    // Save in to userdefault
    let section = sender.tag / 100
    let row = sender.tag % 100
    let dict = arrayCategory[section] as? NSDictionary
    let subLimit = getStringValueFromAPIValue(apiValue: dict?.value(forKey: "limit"), defaultValue: "")

    let cell = self.tableView.cellForRow(at: IndexPath(row: row, section: section)) as! CustomizationDropDownTableViewCell

    if self.section_quantity[section] < Int(truncating: subLimit.numberValue!) {
        cell.btnDropdown.becomeFirstResponder()
    }
}

【问题讨论】:

  • 尝试安全地打开单元格。
  • 它不起作用@SavcaMarin
  • 它没有修复崩溃,或者它没有将您带到顶部?
  • 当我滚动到底部时它没有移动到顶部。
  • 哪条线路崩溃了?提供更多细节。此外,我几乎可以肯定,您必须先滚动到单元格 tableView.scrollToRow(at: indexPath, at: .top, animated: false),然后才能加载单元格及其所有 UI 组件。

标签: ios swift uitableview tableview


【解决方案1】:
let subLimit = getStringValueFromAPIValue(apiValue: dict?.value(forKey: "limit"), defaultValue: "")
let cell = self.tableView.cellForRow(at: IndexPath(row: row, section: section)) as! CustomizationDropDownTableViewCell

我不确定是什么问题导致您的应用崩溃。尝试在代码中设置一些断点。我认为可选问题可能存在问题。

subLimit 是可选的,因此在强制展开时确保它有一个值。 Int(截断:subLimit.numberValue!)

此外,仅当您确定向下转换将始终成功时,才使用强制类型转换运算符(as!)。 请参阅 Apple 的 Swift 文档。 https://docs.swift.org/swift-book/LanguageGuide/TypeCasting.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-03
    • 2016-08-28
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    相关资源
    最近更新 更多