我一直收到NSInternalInconsistencyException错误,但我正在注册一个自定义单元格。我正在尝试向UICollectionViewController
添加UIPageViewController
。我错过了什么?看来UIPageViewController
正在创建我的类的一个新实例,当我打印出传递给cellForItemAt
IndexPath
的对象时,内存地址是两个不同的内存地址,如果它是相同的class,我假设它将是相同的内存地址。
<UICollectionView: 0x7fa89019a600; frame = (0 0; 300 300); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x604000a54dc0>; layer = <CALayer: 0x604000e23e60>; contentOffset: {0, -20}; contentSize: {300, 70}; adjustedContentInset: {20, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7fa88ef8bb30>
DILLY 2
<UICollectionView: 0x7fa890153600; frame = (0 0; 300 300); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x604000a53e60>; layer = <CALayer: 0x604000e21e80>; contentOffset: {0, -20}; contentSize: {300, 70}; adjustedContentInset: {20, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7fa88ef8b6f0>
2017-12-07 00:24:17.043446-0600 Peeke[52042:9993894]
在UIPageViewController的父级中我只是添加了控制器。
viewWillAppear:
self.addChildViewController(self.venueProjector)
self.view.addSubview(self.venueProjector.view)
任何帮助将不胜感激。
class VenueProjector: UIPageViewController, UIPageViewControllerDataSource {
var venue: Venue!
override init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : Any]? = nil) {
super.init(transitionStyle: style, navigationOrientation: navigationOrientation, options: options)
}//end init
convenience init(Venue: Venue){
self.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
self.venue = Venue
}//end init
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}//end init
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.view.backgroundColor = UIColor.white
self.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
let viewControllers = [MarkFeedCollectionView(Venue: venue)]
self.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
}//end func
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let col = MarkFeedCollectionView(Venue: venue)
return col
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let col = MarkFeedCollectionView(Venue: venue)
return col
}
}//end class
class MarkFeedCollectionView: UICollectionViewController, UICollectionViewDelegateFlowLayout{
let cellID: String = "FeedCell"
var marks = [Mark]()
var venueID: String!
var markFeed: UICollectionView = {
let layout: UICollectionViewFlowLayout = {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
flowLayout.itemSize = CGSize(width: 1, height: 1)
flowLayout.estimatedItemSize = CGSize(width: 1, height:1)
return flowLayout
}()
var colview = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
colview.backgroundColor = UIColor.clear
colview.showsVerticalScrollIndicator = false
colview.translatesAutoresizingMaskIntoConstraints = false
return colview
}()
// Do any additional setup after loading the view, typically from a nib.
override init(collectionViewLayout layout: UICollectionViewLayout) {
super.init(collectionViewLayout: layout)
}
convenience init(Venue: Venue){
let layout: UICollectionViewFlowLayout = {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
flowLayout.itemSize = CGSize(width: 1, height: 1)
flowLayout.estimatedItemSize = CGSize(width: 1, height:1)
return flowLayout
}()
self.init(collectionViewLayout: layout)
self.marks.append( Mark(name: Venue.venueName, ID: Venue.VENUE_ID, message: Venue.getVenueDeal()))
self.venueID = Venue.VENUE_ID
self.view.addSubview(self.markFeed)
self.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
self.markFeed.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
self.markFeed.delegate = self
self.markFeed.dataSource = self
}//end convenience init
override func viewDidLoad() {
super.viewDidLoad()
self.markFeed.register(FeedCell.self, forCellWithReuseIdentifier: cellID)
getUpdatedMarks()
}//end func
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}//end init
func populateCollectionView(mark: Mark){
let indexPath = IndexPath(item: marks.count, section: 0)
self.marks.append(mark)
self.markFeed.insertItems(at: [indexPath])
let item = markFeed.numberOfItems(inSection: 0) - 1
let lastItemIndex = NSIndexPath(item: item, section: 0)
markFeed.scrollToItem(at: lastItemIndex as IndexPath, at: UICollectionViewScrollPosition.bottom, animated: true)
}//end func
internal override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.marks.count
}//end func
internal override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print(collectionView)
let liveFeedCell: FeedCell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cellID, for: indexPath) as! FeedCell
liveFeedCell.populateMark(mark: marks[indexPath[1]], venueID: self.venueID)
liveFeedCell.likeButton?.setImage(#imageLiteral(resourceName: "likeButton_notToggled"), for: .normal)
markFeed.layoutIfNeeded()
return liveFeedCell
}//end func
internal func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let message = marks[indexPath[1]].message
if message.count >= 141{
let multiplier = message.count/141
let height = CGSize(width: 0, height: multiplier * 70).height
return CGSize(width: collectionView.frame.width, height: height)
}else{
return CGSize(width: collectionView.frame.width, height: 70)
}
}//end func
private func getUpdatedMarks(){
let Webservice = WebService()
let paramaters: Parameters = ["venueID": self.venueID]
Webservice.getRequestToServer(route: "mark", parameters: paramaters){responseObj, error in
let arr = JSON(responseObj as Any)
let markArr = arr["marks"]
if markArr.isEmpty{
return
}
for mark in markArr{
let (_ , m) = mark
let indexPath = IndexPath(item: self.marks.count, section: 0)
self.marks.append(Mark(mark: m))
self.markFeed.insertItems(at: [indexPath])
};
};
}//end func
}//end class