ios - UITableView 的问题

标签 ios swift cocoa-touch swift2

因此,我在我的 ViewController 中建立了一个 UITableView,并且 UITableView 值是使用数组填充的。每当用户触摸其中一个单元格时,他们就应该被带到另一个 View Controller 。好吧,当应用程序加载并单击第一个单元格时,什么也没有发生,如果单击第二个单元格,则会出现与第一个单元格关联的 View Controller ,然后它就从那里走下坡路。我似乎无法找到解决问题的方法,所以我想我会问问可能能够为我指明正确方向的人。这是相关的代码。提前致谢!

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

private let choices =  ["About this App", "Touch Counting", "Slider Counting", "Calculator", "Date Picker"]
private let simpleTableID = "simpleTableID"

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return choices.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier(simpleTableID)

    if cell == nil {
        cell = UITableViewCell(style: .Default, reuseIdentifier: simpleTableID)
    }
    cell?.textLabel?.text = choices[indexPath.row]
    return cell!
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    switch(indexPath.row) {
        case 0:
            let vc = storyboard?.instantiateViewControllerWithIdentifier("AboutThisApp")
            presentViewController(vc!, animated: true, completion: nil)
        case 1:
            let vc = storyboard?.instantiateViewControllerWithIdentifier("TouchCounting")
            presentViewController(vc!, animated: true, completion: nil)
        case 2:
            let vc = storyboard?.instantiateViewControllerWithIdentifier("SlidingCounter")
            presentViewController(vc!, animated: true, completion: nil)
        case 3:
            let vc = storyboard?.instantiateViewControllerWithIdentifier("Calculator")
            presentViewController(vc!, animated: true, completion: nil)
        case 4:
            let vc = storyboard?.instantiateViewControllerWithIdentifier("Date")
            presentViewController(vc!, animated: true, completion: nil)
        default:
            NSLog("NEVER!")
    }
}
}

最佳答案

改变:

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

到:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

didSelectRowAtIndexPath 在用户选择表格 View 行时被调用,但是 didDeselectRowAtIndexPath 在取消选择行时被调用,一旦用户点击第二行。

关于ios - UITableView 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36296762/

相关文章:

objective-c - iOS5, Storyboard,ARC : Weird categories issue

iphone - 使用委托(delegate)切换多个 View

html - Xcode模拟器,HTML网页发送短信链接无效,为什么?

ios - Swiftui - UIHostingController 添加不需要的导航栏

json - 从 post 方法中检索 json 数据并附加到数组

ios - inputAccessoryView 卡在屏幕底部

ios - 如何将自定义项目添加到导航栏

ios - 设备锁定时可以处理通知操作吗?

ios - 如何在继续之前强制用户与 UIView 交互?

ios - UITableView 在我的自定义单元 UILabel 和重用问题中无法正确显示数据