簡體   English   中英

在didSelectRowAtIndexPath時更改視圖控制器

[英]Changing View Controller when didSelectRowAtIndexPath

我目前正在為iPhone開發錯誤處理服務應用程序的項目。 在我的應用程序中,我有一個表格視圖,當用戶從Adobe Reader或其他地方共享文檔並重新打開該應用程序時,系統將通過附件視圖提示他們如何附加文件。 他們可以將文件添加到表視圖上的現有項目,也可以創建新票證。 如果用戶選擇將附件添加到現有項目,則他們必須選擇該選項,然后單擊表中要添加附件的單元格。 單擊該單元格時,應將另一個視圖控制器添加到當前視圖子視圖中。 我之所以不顯示它,是因為我的標簽欄不是標簽欄項目,因此不會顯示在顯示的視圖中。 但是,當我單擊單元格以附加文件時,出現致命錯誤:索引超出范圍

 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)

        }

    }


}

錯誤恰好發生在

replyView!.didMoveToParentViewController(self)

奇怪的是,最后一個else語句中顯示entryDetailView的代碼也很好用。

這是我的回復ViewController加載時確實出現了

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")


}

我已經在這個問題上停留了兩個小時,所以我們將不勝感激! :) btw,使用xcode 7.3和swift 2.2

您忘記了調用超類:super.viewWillAppear(animated)super.viewDidDisappear r(animated)

我發現了問題,我沒有在回復視圖中更新用於我的主題數組的索引

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM