【问题标题】:UITableViewCell very slow response on selectUITableViewCell 对选择的响应非常慢
【发布时间】:2015-10-30 00:36:16
【问题描述】:

我有一个带有基本单元格的简单 UITableViewController。 didSelectRowAtIndexPath 做简单的工作 - 只需制作 UIAlertView 并显示它。

问题是当我点击一行时,有时我会立即看到警报,有时会在几秒钟后(最长 10 秒)。

代码是

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    // Configure the cell...
    cell.textLabel?.text = "\(indexPath.row)"
    return cell
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    NSLog("row clicked at index \(indexPath.row)")
    let alert = UIAlertView(title: "Test", message: "Test message", delegate: self, cancelButtonTitle: "Done")
    alert.show()
    NSLog("alert showed")
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}

在日志中我看到了

2015-08-06 20:51:54.591 experimental[10323:8602172] row clicked at index 2
2015-08-06 20:51:54.595 experimental[10323:8602172] alert showed
2015-08-06 20:52:00.901 experimental[10323:8602172] row clicked at index 3
2015-08-06 20:52:00.905 experimental[10323:8602172] alert showed

但实际上屏幕上没有显示警报。

任何可以找到解决方案的建议或指示将不胜感激。

【问题讨论】:

  • 您的代码运行良好。
  • 你应该用仪器检查。这段代码看起来不错。
  • 实际上 UIAlertView 已被弃用。使用 UIAlertController。
  • 可能是 xCode 版本 6.4 (6E35b) 的问题,但我在真实设备上也有同样的问题,不仅在模拟器中
  • 谢谢,stosha,但 UIAlertView 中没有问题。任何代码都有相同的效果。

标签: ios swift uitableview uikit


【解决方案1】:

解决方法很奇怪

替换

cell.selectionStyle = UITableViewCellSelectionStyle.None

cell.selectionStyle = UITableViewCellSelectionStyle.Default

彻底解决问题。之后每次点击行都会立即显示结果。

【讨论】:

  • 真的很奇怪。我使用 UITableViewCellSelectionStyleBlue,并且有同样的问题。更改为 UITableViewCellSelectionStyle.Default 对我有用。
  • 这很奇怪!这也适用于我,谢谢队友。任何想法?如果必须摆脱选择样式,我们需要做什么。
  • 这太荒谬了。有谁知道为什么?
  • 天啊!因为这个我发疯了......我在 2 个视图控制器之间进行了自定义转换,我认为我的代码是缓慢转换的罪魁祸首。非常感谢!
【解决方案2】:

将其放入 DispatchQueue.main.async 函数中。

DispatchQueue.main.async{

let alert = UIAlertView(title: "Test", message: "Test message",delegate: self, cancelButtonTitle: "Done") alert.show() NSLog("alert showed") }

replace cell.selectionStyle = UITableViewCellSelectionStyle.None cell.selectionStyle = UITableViewCellSelectionStyle.Default

【讨论】:

    【解决方案3】:

    如果您希望 selectionStyle 为 None,则应将 alertView 方法添加到 dispatch_async(dispatch_get_main_queue(),^{...}); 或将selectionStyle 设置为默认值。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,绝对是一个错误。在我的情况下,它在加载视图之前增加了 150 毫秒。

      我有一个自定义表格单元格

      cell.selectionStyle = UITableViewCellSelectionStyle.None
      

      改成

      cell.selectionStyle = UITableViewCellSelectionStyle.Default
      

      解决了问题...

      【讨论】:

        【解决方案5】:

        我发现如果单元格上有图像,使用手势比选择委托更好

        所以

        iv.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        
        tap.numberOfTapsRequired = 1;
        [iv addGestureRecognizer:tap];
        

        【讨论】:

          【解决方案6】:

          在我的情况下,我需要选择样式.none,因为我使用自定义单元格。因此我必须使用didHighlightRowAtdidUnhighlightRowAt。当您使用.default 时,您无法处理这些事件。

          对我来说 DispatchQueue.main.async{} 成功了!

          【讨论】:

            【解决方案7】:

            尝试设置:

            delaysContentTouches = false
            

            【讨论】:

              猜你喜欢
              • 2014-08-07
              • 1970-01-01
              • 1970-01-01
              • 2012-02-12
              • 1970-01-01
              • 2021-07-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多