【问题标题】:App crashes when I long press on UITableView Custom Header Cell当我长按 UITableView 自定义标题单元格时应用程序崩溃
【发布时间】:2015-06-11 14:50:26
【问题描述】:

我有一个简单的UITableViewController,其中包含一组数据、员工的名字和姓氏。我创建了一个自定义标题原型单元格,只有一个标签来设置标题标题。

但问题是,当用户“长按”表格上的标题单元格时,应用程序崩溃。

在附加的屏幕截图中,Header0、Header1、Header3、Header4 在“长按”时,应用程序崩溃。 然而,红色椭圆标记的标题部分是一个简单的 UIView 标题,但没有任何标签或任何控件。奇怪的是,如果用户“长按”这个空标题,应用程序不会崩溃。

标题视图的代码

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        if ( section == 2 ){
            var emptyView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 50))
            return emptyView
        }

        let cell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! UITableViewCell

        let c = cell as! HeaderTableViewCell
        c.contentView.backgroundColor = UIColor.darkGrayColor()
        c.headerTextLabel.text = "Header" + toString(section)
        return c.contentView
    }

想知道发生了什么。我最近将 Xcode 更新为 6.3,其中包含 Swift 1.2。如何解决这个问题?

非常感谢任何帮助

【问题讨论】:

  • 你有没有为标题实现过任何手势?
  • 你能发布崩溃的完整回溯吗?
  • 未实现任何手势。它只是一个简单的表格视图控制器。
  • @JAL: 也没有任何踪迹。它只是空的“线程 1:EXC_BAD_ACCESS (code=1, address=0x19)
  • 当你在 lldb 中遇到崩溃时,输入 bt all 并发布结果。

标签: ios swift uitableview ios8


【解决方案1】:

我通过移除单元格内容视图 (c.contentView) 上的手势来修复它。

 if let recognizers = c.contentView.gestureRecognizers
  {
  for recognizer in recognizers {
  c.contentView.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
  }
  }

【讨论】:

    【解决方案2】:

    您也可以从这里尝试解决方案:https://stackoverflow.com/a/31877323/67667 即禁用标题单元格的用户交互: c.contentView.userInteractionEnabled = false

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 2019-04-15
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 2015-02-21
      相关资源
      最近更新 更多