【问题标题】:iOS Swift Custom Cell NavigationControlleriOS Swift 自定义单元格导航控制器
【发布时间】:2016-08-24 08:01:58
【问题描述】:

我在 TableView 中有 CollectionView。一切都好但是。当我想将我的单元格导航到另一个 viewController 时出现错误

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){

    let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())

    let bookView: BookSingleController = storyboard.instantiateViewControllerWithIdentifier("BookSingle") as! BookSingleController

    self.navigationController?.pushViewController(bookView, animated: true)


    bookView.bookID = "\(self.books[indexPath.row].id)"

    print(self.books[indexPath.row].id)
}

Xcode 在 self.navigationController?.pushViewController(bookView, animated: true) 行上显示错误。这是错误描述:

Value of 'RelatedBookTableViewCell' has no member 'navigationController'

RelatedBookTableViewCell 是我的自定义单元格类:

class RelatedBookTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout

我的问题在哪里?

谢谢。

【问题讨论】:

  • 您可以使用自定义委托,以便 UICollectionView 告诉 UITableView,后者将告诉其 UINavigationController(通过它是 UIViewController)它需要推送一个新的视图控制器。
  • 一个单元格不应充当数据源或代表其他单元格。这是控制器的工作。如果您确实想保留此设置,请将单元格选择委托给 UIViewController 实例。单元格根本没有导航控制器属性。
  • @Larme 我在我的 RelatedBookTableViewCell 类中使用 UINavigationControllerDelegate 但错误仍然存​​在。

标签: ios objective-c swift uitableview uicollectionviewcell


【解决方案1】:

您将需要:

  1. 使用一些人建议的委托模式。

您将collectionView 委托方法didSelectItemAtIndexPath 的调用传递给您自己的单元格上的自定义委托/协议,其中显示单元格的UITableViewController 是委托给的。这可以在 cellForRowAtIndexPath 中设置。

迅速

自定义协议

protocol DisplayBookDelegate { func displayBook(bookId: String) }

在tableViewController中

class tableViewController: UITableViewController, DisplayBookDelegate {

    ...

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.delegate = self
        return UITableViewCell()
    }

    func displayBook(let bookId) {
        self.navigationController?.pushViewController(bookView, animated: true)
    }
}

在单元格中

var delegate: DisplayBookDelegate?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.delegate.displayBook("1")
}
  1. 使用通知并在通知对象的字典中传递bookId

objc[[NSNotificationCenter defaultCenter] postNotificationName:"kDisplayBookView" object:@{"bookId":"1"}];

迅速 NSNotificationCenter.defaultCenter().postNotificationName("kDisplayBookView", object: ["bookId":"1"])

【讨论】:

    【解决方案2】:

    使用下面的代码,您可以获得顶部视图控制器,并使用该视图控制器,您可以执行推送操作

      let objViewController = UIApplication.topViewController()!
    

    使用这个扩展

    extension UIApplication {
        class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
            if let nav = base as? UINavigationController {
                return topViewController(nav.visibleViewController)
            }
            if let tab = base as? UITabBarController {
                if let selected = tab.selectedViewController {
                    return topViewController(selected)
                }
            }
            if let presented = base?.presentedViewController {
                return topViewController(presented)
            }
            return base
        }
    }
    

    【讨论】:

      【解决方案3】:

      尝试从应用程序的根控制器推送 VC。

      UIApplication.sharedApplication().keyWindow?.rootViewController
      

      【讨论】:

      • 谢谢亲爱的 Edder Núñez,你能解释一下吗?
      • 当您嵌套 UIViews 时,很难跟踪导航控制器。但是您可以从代码中的任何位置从应用程序本身访问 root,因为它是单例。在那里你可以推 VC。
      猜你喜欢
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      相关资源
      最近更新 更多