ios - 以编程方式设置 UITableView 行选择返回 nil

标签 ios swift uitableview

尝试调用要以编程方式选择的 TableView 行。

我的所有委托(delegate)均已正确设置,并且 UITableView 已链接到 IB 中。所有其他 UITableView 方法都可以正常工作,并且位于主 ViewController 或其扩展中。当调用以下内容时,我得到Thread 1: Fatal error: Unexpectedlyfound nil while unwrapping anOptional value

func selectTableViewRowProgrammatically () {

    let indexPath = IndexPath(row: someVariable, section: 0)
    tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableView.ScrollPosition.none)
    tableView.delegate?.tableView!(self.tableView, didSelectRowAt: indexPath)

    tableView.reloadData()
}

self.tableView.selectRow(at:indexPath,animated:false,scrollPosition:UITableView.ScrollPosition.none)上显示错误

谢谢!

最佳答案

你可以尝试下面的代码吗

let indexPath = IndexPath(row: someVariable, section: 0)

      UIView.animate(withDuration: 0.1, animations: {
                   tableView.scrollToRow(at: indexPath, at: .top, animated: false) 

                }) { (true) in
                    tableView.delegate?.tableView!(self.tableView, didSelectRowAt: indexPath)

                }

关于ios - 以编程方式设置 UITableView 行选择返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54357229/

相关文章:

ios - 具有相同 xib 的两个不同的 UITableViewCell

iphone - 使用 javascript 或 url 检测 iPhone 上已安装的应用程序

ios - swift : Background Color fading animation (SpriteKit)

ios - 如何隐藏导航栏后退按钮中的后退箭头?

ios - UITableView 底部分隔符插图 - IOS 7 中的奇怪行为

ios - libc++abi.dylib : terminating with uncaught exception of type NSException (CollectionView)

ios - 如何实现交互式单元格滑动以触发过渡动画,如 WhatsApp

swift - CLLocationManager 已启用但选中从不

ios - UILabel 在 cellForRowAtIndexPath 中为 nil

ios - 解析 RSS Feed 的摘要导致应用程序崩溃