仅在iPhone 4S上使用postNotification后添加了观察者
问题描述:
我有一个功能完整的代码,可以在iPhone> 5和所有iPad上完美运行,其中我通过观察者/通知传递信息和调用功能,但在iPhone 4S上它只是不起作用。仅在iPhone 4S上使用postNotification后添加了观察者
经过调试,我实际上是ONLY发现,在iPhone 4S上运行的观察者会添加后通知被张贴一段时间。
这发生在设备和模拟器上,分别在iOS 8和9上。
代码:
**PostNotification**
override func viewDidLoad() {
super.viewDidLoad()
self.registerCells()
UIApplication.sharedApplication().statusBarStyle = .LightContent
self.collectionView.delaysContentTouches = false
NSNotificationCenter.defaultCenter().addObserver(self, selector: "footerUpdateContentSize:", name: "footerUpdateContentSize", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "seasonUpdateContentSize:", name: "seasonUpdateContentSize", object: nil)
self.loadDetailTVShow()
}
func registerCells()
{
self.collectionView.registerClass(HeaderDetailSeriesCell.self, forCellWithReuseIdentifier: "HeaderDetailSeriesCell")
self.collectionView.registerClass(FooterDetailSeriesCell.self, forCellWithReuseIdentifier: "FooterDetailSeriesCell")
self.collectionView.registerClass(SeriesSeasonContentCell.self, forCellWithReuseIdentifier: "SeriesSeasonContentCell")
}
func loadDetailTVShow()
{
let id: String! = (tvShow != nil) ? tvShow!.id! : episode!.seriesId!
ContentsClient().getContentById(id!).then { data -> Void in
let m: TVShow? = data as! TVShow?
self.tvShow = m!
self.collectionView.reloadData()
NSNotificationCenter.defaultCenter().postNotificationName("loadedTvShowlist", object: self.tvShow)
UIView.animateWithDuration(1.0, delay: 0, options: .TransitionNone, animations:
{
self.loadingView.alpha = 0.0
}, completion:nil)
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
cell = collectionView.dequeueReusableCellWithReuseIdentifier("SeriesSeasonContentCell", forIndexPath: indexPath)
let seriesContent = cell as! SeriesSeasonContentCell
seriesContent.seriesContentCell?.tvShow = self.tvShow
seriesContent.contentView.frame = CGRect(x: 0.0, y: 0.0, width: width, height: heightSeason)
seriesContent.seriesContentCell?.view.frame = seriesContent.contentView.frame
}
**Observer**
class SeriesSeasonContent
override func viewDidLoad() {
super.viewDidLoad()
self.registerCells()
seasonSelectedIndex = 0
collectionView.reloadData()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadedTvShowlist:", name: "loadedTvShowlist", object: nil)
}
func registerCells()
{
self.seasonCollection.registerClass(SeasonViewCell.self, forCellWithReuseIdentifier: "SeasonViewCell")
self.collectionView.registerClass(EpisodeViewCell.self, forCellWithReuseIdentifier: "EpisodeViewCell")
}
func loadedTvShowlist(notification : NSNotification){
self.tvShow = notification.object! as? TVShow
if (tvShow?.seasons != nil && tvShow?.seasons?.count > 0)
{
self.currentSeason = ((tvShow?.seasons!.objectAtIndex(seasonSelectedIndex) as? Season)?.episodes)!
self.collectionView.reloadData()
self.seasonCollection.reloadData()
}
}
观测数据:进出口使用在同一视图中的多个collectionviews和笔尖文件被正确命名
没有任何理由为什么iPhone 4S确实加载viewDidLoad方法通知发布之后?
答
管理解决我的问题。 iPhone 4s的屏幕尺寸不足以触发cellForIndex事件,直到手机屏幕向下滚动,才启动类/观察者。
验证您的通知是否在违规平台的主线程中发布。该块('ContentsClient()。getContentById(id!)。然后{'似乎是一个数据获取回调,你也在其中做各种UI。 –
是的,它被张贴在主线程上,那是什么这是一个数据获取回调,但即时通讯使用处理这些请求的PromiseKit窗格,并且只有当请求结束时.then块才会被执行。 –