【问题标题】:programmatic UITableView's didSelectRowAtIndexPath not responding to touch编程 UITableView 的 didSelectRowAtIndexPath 没有响应触摸
【发布时间】:2015-06-05 01:44:36
【问题描述】:

我在默认的 KeyboardViewController.swift 中以编程方式为自定义键盘扩展创建了 UITableView,但由于某种原因,didSelectRowAtIndexPath 根本没有响应。我确保将表格和单元格的 userInteractionEnabled 设置为 true,并确保将 allowSelectionDuringEditing 设置为 true。 tableView 肯定已被填充,当我触摸它们时单元格会突出显示,但日志根本没有出现(当它上有断点时方法也不会中断)。

有人对我为了实现这一目标而搞砸了什么有什么想法吗?

class KeyboardViewController: UIInputViewController, UITableViewDelegate, UITableViewData Source {

 var tableView: UITableView = UITableView()
 //other setup

override func viewDidLoad() {
  super.viewdidLoad

  // other setup here

  tableView.frame = CGRectMake(0, 0, 320, 160)
  tableView.delegate = self
  tableView.dataSource = self
  tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
  tableView.allowsSelection = true
  tableView.allowsSelectionDuringEditing = true
  tableView.userInteractionEnabled = true
  self.view.addSubview(tableView)
 }

//delegate&datasource methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let sectionTitle = artistSectionTitles[section]
    let sectionArtist = artists[sectionTitle]

    return sectionArtist!.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    cell.userInteractionEnabled = true
    let sectionTitle = artistSectionTitles[indexPath.section]
    let sectionArtists = artists[sectionTitle]
    let artist = sectionArtists?[indexPath.row] as NSString

    cell.textLabel?.text = artist
    return cell
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 22
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
    println("You selected cell #\(indexPath.row)!")
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return artistSectionTitles[section]
}

func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
    return artistSectionTitles
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return artistSectionTitles.count
}

【问题讨论】:

    标签: ios uitableview swift didselectrowatindexpath


    【解决方案1】:

    您如何跟踪选择实际上不起作用?因为如果行点亮 - 它应该可以工作。

    也许您正在某处更改 tableView 委托?

    还可以尝试更改单元格 selectionStyle 属性。

    【讨论】:

    • 我在 didSelectRowAtIndexPath 中设置了一个断点,并且还有一个未记录的 println 语句:\
    【解决方案2】:

    显然,使用新的 iOS 应用扩展类型进行调试可能非常粗略。为了在 didSelectRowAtIndexPath 中命中调试断点,我遵循了 Alex Guerrero 在此 stackoverflow 帖子中的回答。希望它能帮助其他人使用新的应用程序扩展!

    https://stackoverflow.com/a/27352955/3282316

    复制粘贴在下方,以防帖子被删除

    Apple 已在 Xcode 6.1 发行说明中将此问题报告为已知问题:

    iOS 8.1 模拟器中的 Safari、地图和开发者应用程序无法正确支持本地化和键盘设置,包括第 3 方键盘。 [NSLocale currentLocale] 返回 en_US 并且只有英文和表情符号键盘可用。 (18418630, 18512161) 我正在使用 Xcode 6.1.1,似乎还没有解决,但我发现了一种解决方法。您应该按照以下步骤操作:

    打开 Xcode 并在其菜单栏中点击 Xcode > Open Developer Tool > iOS Simulator

    现在进入 iOS 模拟器菜单栏,转到硬件 > 键盘并检查是否启用了“连接硬件键盘”。如果是,请单击它以禁用并退出 iOS 模拟器

    然后返回 Xcode 并确保选择键盘作为方案

    单击运行按钮或按产品 > 运行以构建并运行您的应用程序,然后在“选择要运行的应用程序”菜单中选择今天

    当 iOS 模拟器启动时,转到设置 > 常规 > 键盘 > 键盘 > 添加新键盘并选择您自己的,然后按键盘命令 + shift + H 返回主屏幕

    从这里我回到我的应用程序并单击一个单元格,didSelectRowAtIndexPath 响应了!

    如果键盘仍然没有出现: 从 ~/Libray/Preferences/ 中删除文件 com.apple.iphonesimulator.plist 在 iOS 模拟器菜单栏中点击进入 iOS 模拟器 > 重置内容和设置...

    【讨论】:

      【解决方案3】:

      您可能对 TableView 的选择属性给出了“无选择”。请更改并尝试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-04
        • 2011-03-19
        相关资源
        最近更新 更多