在 didSelectRowAtIndexPath 时更改视图控制器
Posted
技术标签:
【中文标题】在 didSelectRowAtIndexPath 时更改视图控制器【英文标题】:Changing View Controller when didSelectRowAtIndexPath 【发布时间】:2016-07-11 20:10:39 【问题描述】:我目前正在为 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")
我已经在这个问题上停留了几个小时,所以任何帮助都将不胜感激! :) 顺便说一句,使用 xcode 7.3 和 swift 2.2
【问题讨论】:
另外,表格视图是自定义表格视图,因此单元格可以展开和折叠 【参考方案1】:你忘了调用超类:super.viewWillAppear(animated) super.viewDidDisappear r(动画)
【讨论】:
【参考方案2】:我发现了问题,我没有在回复视图中更新用于我的主题数组的索引
【讨论】:
以上是关于在 didSelectRowAtIndexPath 时更改视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
tableView - didSelectRowAtIndexPath 未调用
在 didSelectRowAtIndexPath 中呈现缓慢的 ViewController
我可以麻烦UITableViewDelegate的didSelectRowAtIndexPath:吗?
在 didselectRowAtIndexPath 之后 UITableView 无法滚动底部