程序化的UITableView的didSelectRowAtIndexPath没有响应触摸

时间:2015-03-31 16:13:29

标签: ios uitableview swift didselectrowatindexpath

我在默认的KeyboardViewController.swift中以编程方式为自定义键盘扩展创建了一个UITableView,但由于某种原因,didSelectRowAtIndexPath根本没有响应。我确保将单元格的userInteractionEnabled设置为true,并确保将allowsSelectionDuringEditing设置为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
}

3 个答案:

答案 0 :(得分:0)

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

也许你正在改变某个地方的tableView委托?

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

答案 1 :(得分:0)

显然,使用新的iOS App扩展类型进行调试可能非常粗略。要在didSelectRowAtIndexPath中点击调试断点,我跟随Alex Guerrero的回答来自这个stackoverflow帖子。希望它能帮助其他人使用新的应用扩展程序!

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

复制并粘贴到下方以防邮件被删除

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

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

打开Xcode并在其菜单栏中点击进入Xcode>打开开发人员工具> iOS模拟器

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

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

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

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

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

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

答案 2 :(得分:0)

你可能已经给出了#34;没有选择"到TableView的选择属性。请改变它并尝试。

相关问题