如何克服在表中敲击一行

时间:2015-07-31 05:43:04

标签: ios iphone xcode swift

当我在表格中点击一行时,它不会显示下一个屏幕,但是当我点击另一行时,它会显示之前点击的行的详细信息。 如何解决这个问题?

class AllListsViewController: UITableViewController {

    var lists: [Checklist]

    required init(coder aDecoder: NSCoder)
    {
        lists = [Checklist]()

        super.init(coder: aDecoder)

        var list = Checklist(name: " Birthdays")
        lists.append(list)

        list = Checklist( name: "Groceries")
        lists.append(list)

        list = Checklist(name: "Cool Apps")
        lists.append(list)

        list = Checklist(name: "To Do")
        lists.append(list)


    }

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

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

        let checklist = lists[indexPath.row]

        performSegueWithIdentifier("ShowChecklist", sender: checklist)
    }




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

        return lists.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cellIdentifier = "cell"

        var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)

        if cell == nil
        {
            cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
          }

          let checklist = lists[indexPath.row]
          cell.textLabel!.text = checklist.name
          cell.accessoryType = .DetailDisclosureButton

           return cell

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "ShowChecklist"
        {
            let controller = segue.destinationViewController as! ChecklistViewController

            controller.checklist = sender as! Checklist
        }
    }


    }

这是我的didSelectRowAtIndex。

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


        if let cell = tableView.cellForRowAtIndexPath(indexPath)
    {
            let item = items[indexPath.row]

            item.toggleChecked()

            configureCheckmarkForCell(cell, withChecklistItem: item)

        }

        tableView.deselectRowAtIndexPath(indexPath, animated: true)

            saveChecklistItems()

    }

1 个答案:

答案 0 :(得分:0)

你搞乱了代表的使用。

您在错误的委托方法中调用performSegueWithIdentifier,其中行取消选择

如果您想执行segue,您应该使用tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)方法。

目前,您使用的是tableView (tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)方法。

交换代码后,它应该是这样的:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
  let checklist = lists[indexPath.row]
  performSegueWithIdentifier("ShowChecklist", sender: checklist)
}