我一直没办法解决的 实作collectionView

陳尹
  • 348

小妹现要作一个类似以下的东西,结合一个scrollview 跟 collectionview ,我的scorllview是实作成功了,但collectview不管怎么弄都没办法成功显示,请教各位牛人

图片描述

我的 code 如下,因为我不是用stroybord去定义 画面,上面的部份,网路的截取这方面都没问题,问题是下方的 collectview ,不知道是那里出错,总是有以下的错误讯息
我怕程式太长有关scrollview的地方我就不删掉了,只留有问题的collectionview

图片描述

我怕程式太长有关scrollview的地方我就不删掉了,只留有问题的collectionview

   class ViewController: UIViewController ,UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
var banner_scrollView:UIScrollView!
var banner_pageControl:UIPageControl!
var promoList:UICollectionView!
var promoLayer:UICollectionViewFlowLayout!
   // var m_myImg:UIImageView!
var myImgAry:[UIImageView]=[]
var testURL:NSURL!
var postString:String = ""
let myFunc:MyClass! = MyClass()
let webURL:NSString = "http://xxxxx/banner/";
var webPhoto = Array<UIImageView>()
var bannerH:CGFloat = 0.0
var bannerW:CGFloat = 0.0
var pageViewW:CGFloat!
var pageViewH:CGFloat!
var pageView:UIView!
var horizontalBlank:CGFloat = 3.0  //畫面上每個可見大塊元件的上間格
var verticalBlank:CGFloat = 2.0
func refreshWithFrame(frame:CGRect){
    self.view.frame = frame
    self.view.backgroundColor = UIColor.lightGrayColor()
    
   
    
    //print(self.navigationItem.)
    banner_scrollView = UIScrollView()
    banner_scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 6 + 70)
   ///下面這一段沒問題/////////////////////////////////////// 
    /*banner_scrollView.contentSize = CGSizeMake(self.view.frame.size.width , 0)
   
    testURL=NSURL(string:"http://xxxxx/ajax.php")
    let request = NSMutableURLRequest(URL:testURL);
    request.HTTPMethod = "POST"
   
    postString = "type=ios&locate=index"
    request.HTTPBody = try! postString.dataUsingEncoding(NSUTF8StringEncoding)
    pageViewW = self.view.frame.size.width;
    pageViewH = bannerH;
    
    
    
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in
        if error != nil {
            print("\(error)") 
            return
        }
        do {
            let myJSON:AnyObject? = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
            self.drawScreen(myJSON!)
        }
        catch {print(error)}
    }
    task.resume()*/
    
    
    
    
  
                  
    
}
func drawScreen(indexJSON:AnyObject){

    
    /////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    以下是我错误的地方,也是我一直没办法解决的 实作collectionView
     以下是我错误的地方,也是我一直没办法解决的 实作collectionView
      以下是我错误的地方,也是我一直没办法解决的 实作collectionView
    /////////////////////////////////////////////////////////////////

    promoList = UICollectionView(frame: CGRectMake(0, 80, self.view.frame.width , 50))
    promoList.backgroundColor = UIColor.whiteColor()
    promoList.delegate = self
    promoList.dataSource = self
    self.view.addSubview(promoList)

    
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let kDisplayCell_ID:String = "DispalyCellID"
    let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(kDisplayCell_ID, forIndexPath: indexPath)
    cell.backgroundColor = UIColor.redColor()
    return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
  return 1
}


}

拜托拜托 救救小妹吧

error message:
2016-07-23 18:59:28.142 shopping[1182:31607] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

回复
阅读 3.9k
3 个回答

layout空的,必须初始化,layout

看你的问题,你给的信息很难分析,个人直觉上感觉是你的缓存那一块的问题,建议你的应该定义成常量const,然后没看见你初始化写在哪里了? 0.0

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

var m_collectionView:UICollectionView!
var m_layout:UICollectionViewFlowLayout!
func refreshWithFrame(frame:CGRect){
    self.view.frame = frame
    self.view.backgroundColor = UIColor.lightGrayColor()
    m_layout = UICollectionViewFlowLayout()
    m_layout.minimumLineSpacing = 1.5
    m_layout.minimumInteritemSpacing = 1
    m_collectionView = UICollectionView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height), collectionViewLayout: m_layout)
    m_collectionView.backgroundColor = UIColor.whiteColor()
    m_collectionView.delegate = self
    m_collectionView.dataSource = self
    m_collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "TESTCell")
    self.view.addSubview(m_collectionView)

}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

/*func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    var reuseView:UICollectionReusableView!
    reuseView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "TestHeader", forIndexPath: indexPath)
    reuseView.backgroundColor = UIColor.blackColor()
    return reuseView
} */

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("TESTCell", forIndexPath: indexPath)
                cell.backgroundColor = UIColor.lightGrayColor()
            return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    //返回每个cell的大小
    return CGSize(width: self.view.frame.size.width / 3 - 10, height:100)
}

    
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
    return UIEdgeInsets(top:0, left: 0, bottom:5, right: 0)
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    //Cell点击事件
}

}

宣传栏