Changing View Controller when didSelectRowAtIndexPath

时间:2016-07-11 20:10:39

标签: ios iphone uitableview uiviewcontroller swift2

I am currently working on a project developing an error handling service app for the iPhone. Within my app I have a table view and when the user shares a document from Adobe reader, or somewhere else, and reopen the app they are prompted with an accessory view on how they would like to attach the file. They can either add the file to an existing item on the table view or create a new ticket. If the user chooses to add the attachment to an existing item, they must choose that option and then click on the cell within the table where they would like to add the attachment. When the cell is clicked another view controller should be added to the current views subview. The reason I am not presenting it is because my tab bar will not show on the presented view as it is not a tab bar item. However, when I click the cell to attach the file I get a fatal error: Index out of range

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

    let topic = thread[indexPath.row]
    let selectedCell = threadTableView.cellForRowAtIndexPath(indexPath)

    if(importFileMode) {

        findChildrenOfParent(thread, childsIndex: indexPath.row)

        let replyView = self.storyboard?.instantiateViewControllerWithIdentifier("replyView")
        self.addChildViewController(replyView!)
        self.view.addSubview(replyView!.view)
        replyView!.didMoveToParentViewController(self)

    }
    else {

        if(topic.canBeExpanded) {

            if(topic.isExpanded) {

                self.collapseCellsFromIndexOf(topic, indexPath: indexPath, threadTableView: threadTableView)
                selectedCell?.accessoryView = self.viewForDisclosureForState(false)

            }
            else {

                self.expandCellsFromIndexOf(topic, indexPath: indexPath, threadTableView: threadTableView)
                selectedCell?.accessoryView = self.viewForDisclosureForState(true)


            }
        }
        else {

            // present the detailed view of entries
            selectedEntryIndex = 0

            findChildrenOfParent(thread, childsIndex: indexPath.row)

            let entryDetailView = self.storyboard?.instantiateViewControllerWithIdentifier("EntryDetailScene")

            self.addChildViewController(entryDetailView!)
            self.view.addSubview(entryDetailView!.view)
            entryDetailView!.didMoveToParentViewController(self)

        }

    }


}

The error occurs precisely at

replyView!.didMoveToParentViewController(self)

Curiously enough, the code in the last else statement displaying the entryDetailView works just fine.

This is my reply ViewController when loading and did appear

override func viewDidLoad() {
    super.viewDidLoad()

    fileButtons = [attachmentButton1, attachmentButton2, attachmentButton3, attachmentButton4]

    self.entryTopicLabel.text? = topicTitle
    self.commentTextView.text? = entryList[selectedEntryIndex].etext!

    replyTextView.layer.borderColor = textViewBorderColor.CGColor
    replyTextView.layer.cornerRadius = 20
    replyTextView.layer.borderWidth = 0.5
    replyTextView.layer.masksToBounds = true
    replyTextView.spellCheckingType = UITextSpellCheckingType.Yes

    replyTextView.delegate = self

    self.cancel = UIBarButtonItem(title: "Cancel ", style: .Plain, target: self, action: #selector(cancelReply))

    self.cancel.tintColor = UIColor.whiteColor()
    self.cancel.setTitleTextAttributes([
        NSFontAttributeName: myFontNormal]
        , forState: UIControlState.Normal)

    self.navItems.rightBarButtonItem = self.cancel
    self.navigationBar.items = [navItems]

    attachmentButton1.hidden = true
    attachmentButton2.hidden = true
    attachmentButton3.hidden = true
    attachmentButton4.hidden = true

}

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

override func viewDidDisappear(animated: Bool) {

    filesToUpload.removeAll()

}

override func viewWillAppear(animated: Bool) {

    if(haveImportedFile) {

        attachImport()

    }

}


func attachImport() {

    let defaults = NSUserDefaults(suiteName: userDefaultsGroup)
    let file = defaults?.objectForKey("file")

    if(filesToUpload.count < 5) {

        filesToUpload.append(file!)

    }
    else {

        singleActionAlert(self, title: "Too Many Attachments", message: "You are only allowed a maximum of four file attachments.", action: "OK")

    }

    defaults?.setObject(nil, forKey: "file")


}

Ive been stuck on this problem for a couple of hours now so any help will be appreciated! :) btw, using xcode 7.3 and swift 2.2

2 个答案:

答案 0 :(得分:0)

You forgot to call superclasses: super.viewWillAppear(animated) super.viewDidDisappear r(animated)

答案 1 :(得分:0)

我发现了这个问题,我没有在回复视图中更新用于我的主题数组的索引